[英]TableView Pushing to Detail View in Swift
我需要將tableView推送到detailView的幫助。 現在,我擁有大部分代碼,但是在我的應用加載時,它轉到了detailedView,並且每當我嘗試返回到tableView時,它都會立即回到detailedView,並且在調試區域中會顯示以下錯誤:
在意外狀態下完成導航過渡。 導航欄子視圖樹可能已損壞。
我不明白為什么會這樣,因為我的代碼不在代碼的viewDidLoad部分中。這是tableView的代碼:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Label.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel.text = Label[indexPath.row]
performSegueWithIdentifier ("showDetail", sender: self)
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "showDetail" {
}
}
return cell
}
如果您可以幫助我使Segue工作達到預期的效果,請在下面留下答案。 謝謝!
PS我也很好奇如何自定義導航欄上的后退按鈕,從“ <返回”到僅“ <”。
PSS如何在按下后使tableViewCell自動釋放,使其不突出顯示? 總體看來,它看起來像一個更好的應用程序。
PSSS對不起,但最后一件事是如何根據用戶單擊的單元格自定義DeatilView? 我已經用文本設置了新類,但不確定如何使if語句為單擊的每個單元格自定義文本。
再次感謝!
錯誤在您的cellForRowAtIndexPath
:
performSegueWithIdentifier ("showDetail", sender: self)
這會在顯示單元格而不是選擇單元格時觸發序列。 修復的最簡單方法是在IB中創建從單元到目標視圖控制器的序列。
另外,此代碼:
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "showDetail" {
}
}
創建一個在cellForRowAtIndexPath
本地的函數,該函數永遠不會執行。 正確的方法是在類范圍內將其聲明為超類實現的替代。
如果您已經在IB中定義了segue,則您的代碼應為:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel.text = Label[indexPath.row]
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
}
}
您必須在tableView(_:didSelectRowAtIndexPath:)
而非tableView(_:cellForRowAtIndexPath:)
執行segue
我發現這個舊問題在目標C( 如何更改后退按鈕上的文本)中有答案
您必須在從中選擇的View Controller中進行設置。
假設我們有一個簡單的iPhone應用程序,它具有一個UITableViewController,其中包含一堆列出數字的單元格,我們將其稱為“ NumberListTVC”。 我們將這個NumberListTVC嵌入了NavigationController中。 我們在一個單元格上將選擇序列設置到另一個ViewController,我們將其稱為“ NumberDisplayerVC”。 此NumberDisplayerVC具有一個標簽,我們將其設置為在NumberList上單擊的標簽的內容。 因此,當您在NumberListTVC中選擇一個單元格時,它將推到NavigationController中的“ NumberDisplayer”。
因此,對於這個應用程序,您需要在NumberListTVC中放入自定義后退按鈕的代碼,以便在查看NumberDisplayerVC時,它將顯示我們想要的后退按鈕,而不是默認按鈕(因為后退按鈕會顯示它返回到NumberListTVC)。
在Objective-C中,根據鏈接的答案,將“ <返回”更改為“ <”的代碼為:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
基本上,我們用所需的標題創建一個新的UIBarButtonItem。 在這種情況下,我只是給它命名為“”,這只是一個空字符串。
Swift中的相同命令為:
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style:.Plain, target: nil, action: nil)
我將其放在NumberListTVC的viewDidLoad方法中,以確保它在加載NumberListTVC時運行。
這在Objective-C中涵蓋了選定的UItableViewCell答案,在選定時保持藍色
您只需要告訴UITableViewCell在其didSelectRowAtIndexPath中取消選擇它即可。
在Objective-C中,您將重寫它並執行以下操作:-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {//做其他您想在這里做的事情... [tableView deselectRowAtIndexPath:indexPath動畫:是]; }
Swift代碼將類似於:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
//Do whatever else you want to do here...
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
為簡單起見,我將使用第一個示例,該示例僅將我們的navigationController與NumberListTVC和NumberDisplayerVC一起使用,因此不在UISplitView上使用,但所提出的問題會影響這兩種情況。
基本上,您必須從被單擊的單元格中獲得所需的內容,然后將其設置為要在下一個ViewController中使用的任何內容。 因此,在您的prepareForSegue中,您將執行以下操作:
if segue.identifier == "amazingSegue"
{
if let cellText = (sender as? UITableViewCell)?.textLabel.text
{
if let someVC = segue.destinationViewController as? NumberDisplayerVC
{
someVC.textToDisplay = cellText
}
}
}
這基本上就是安東尼奧在回答中所說的。 我增加了一點。 因此,就我而言,NumberListTVC是一堆動態創建的UITableViewCells。 我想從每個標簽中抓取標簽,然后在NumberDisplayerVC中使用它。 因此,我們:
cellText
。 我們使用可選的類型強制轉換運算符檢查發送方是否為UITableViewCell。 然后,我們使用可選的鏈接查看此UITableViewCell的內部,並從其textLabel
提取text
。 destinationViewController
分配給常數someVC
。 我們可以選擇將其強制類型轉換為NumberDisplayerVC,這是應該的。 如果確實為1,它將正確綁定到someVC變量。 textToDisplay
屬性設置為我們可選綁定的cellText
常量即可。 我不確定這是否是最好的方法,我對if let
語句的塔感不是很好,但是起初可以完成任務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.