我的performSegueWithIdentifier
方法中有一个didSelectRowAtIndexPath
,但它的行为非常奇怪。我在目标VC中有一个tableView。当按下单元格时,会触发segue并触发目标VC中的viewDidLoad方法,但是我必须再次点击屏幕以使表格视图开始填充并显示VC。
仅当我从didSelectRowAtIndexPath
执行segue时才会发生这种情况,而不是在我从按钮操作执行时。
我发现这很奇怪,无法弄清楚出了什么问题。
编辑:我的didSelectRowAtIndexPath
- 方法:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedCell:CategoryTableViewCell = self.categoriesTV.cellForRowAtIndexPath(indexPath) as! CategoryTableViewCell
if selectedCell.categoryLabel.text == "VW" {
self.performSegueWithIdentifier("chart", sender: self)
self.datatypeToSend = "VW"
self.chartTitleToSend = selectedCell.categoryLabel.text!
}else if selectedCell.categoryLabel.text == "Porche"{
self.performSegueWithIdentifier("chart", sender: self)
self.datatypeToSend = "Porche"
self.chartTitleToSend = selectedCell.categoryLabel.text!
}else if selectedCell.categoryLabel.text == "Lexus"{
self.performSegueWithIdentifier("chart", sender: self)
self.datatypeToSend = "Lexus"
self.chartTitleToSend = selectedCell.categoryLabel.text!
}else if selectedCell.categoryLabel.text == "BMW"{
self.performSegueWithIdentifier("chart", sender: self)
self.datatypeToSend = "BMW"
self.chartTitleToSend = selectedCell.categoryLabel.text!
}else if selectedCell.categoryLabel.text == "Toyota"{
self.performSegueWithIdentifier("chart", sender: self)
self.datatypeToSend = "Toyota"
self.chartTitleToSend = selectedCell.categoryLabel.text!
}
}
编辑2:数据加载不是问题,问题是我是否加载数据,但只有当我从表格视图中触发segue时才会出现问题。
答案 0 :(得分:0)
您应该在执行segue之前设置数据。您的if语句也可以极大地简化。尝试这样的方法:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let selectedCell: CategoryTableViewCell = categoriesTV.cellForRowAtIndexPath(indexPath) as CategoryTableViewCell
// Simplified version of your if-else statement. Notice the data is set before performing the segue as well, which is important.
datatypeToSend = selectedCell.categoryLabel.text ?? ""
chartTitleToSend = selectedCell.categoryLabel.text ?? ""
performSegueWithIdentifier("chart", sender: self)
}