我的问题是我的分段控件不会将信息传递给我的 tableView 中的 cellForRowAtIndexPath 方法。当选择每个选项卡时,我希望我的 TableView 显示不同的数据。
这是我认为问题根源的代码片段:
var parsedDataForTableView : ParseForDetailView? {
didSet {
self.selectedIndex = (parsedDataForTableView?.tabSelected)!
self.tableView.rowHeight = (parsedDataForTableView?.tableViewRowHeight)!
self.tableView.reloadData()
}
}
var selectedIndex = Int()
override func viewDidLoad() {
super.viewDidLoad()
self.parsedDataForTableView = ParseForDetailView(segmentedTabSelected: 0, primaryBarData: self.primaryBarDetails!, secondaryBarData: self.secondaryBarDetails!)
configureView()
configureNavigationBarItems()
setImageShadow()
}
@IBAction func switchBetweenTabs(sender: AnyObject) {
if segmentControl.selectedSegmentIndex == 0 {
print("Drinks Selected")
self.selectedIndex = 0
print(self.selectedIndex)
self.parsedDataForTableView = ParseForDetailView(segmentedTabSelected: 0, primaryBarData: self.primaryBarDetails!, secondaryBarData: self.secondaryBarDetails!)
self.tableView.reloadData()
}
if segmentControl.selectedSegmentIndex == 1 {
print("Entertainment Selected")
self.selectedIndex = 1
print(self.selectedIndex)
self.parsedDataForTableView = ParseForDetailView(segmentedTabSelected: 1, primaryBarData: self.primaryBarDetails!, secondaryBarData: self.secondaryBarDetails!)
self.tableView.reloadData()
}
if segmentControl.selectedSegmentIndex == 2 {
print("Info Selected")
self.selectedIndex = 2
print(self.selectedIndex)
self.parsedDataForTableView = ParseForDetailView(segmentedTabSelected: 2, primaryBarData: self.primaryBarDetails!, secondaryBarData: self.secondaryBarDetails!)
self.tableView.reloadData()
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("detailViewCell") as! DetailViewCell
print(self.selectedIndex)
if self.selectedIndex == 0 {
cell.drinkLabel.text = self.parsedDataForTableView?.drinksArray[indexPath.section][indexPath.row]
cell.priceLabel.text = self.parsedDataForTableView?.priceArray[indexPath.section][indexPath.row]
cell.entertainmentLabel.text = ""
} else if self.selectedIndex == 1 {
cell.entertainmentLabel.text = self.parsedDataForTableView?.entertainmentString
cell.drinkLabel.text = ""
cell.priceLabel.text = ""
} else if self.selectedIndex == 2 {
// finish this...
cell.drinkLabel.text = "Info"
cell.priceLabel.text = "Info"
cell.entertainmentLabel.text = "Info"
}
return cell
}
因此,上面我们将 IBAction 连接到分段选项卡和 cellForRowAtIndexPath 方法。我想将 selectedIndex 属性传递到 cellForRowAtIndexPath 方法中,以在我想要显示的不同数据之间切换 - 在我的 didSet 顶部初始化。
当我运行代码时, View 加载得很好,第一个段的信息(第 0 个段索引)显示正确,并且我的 cellForRowAtIndexPath 中的 print(self.selectedIndex) 为按计划打印的每个单元格打印“0”。当我点击第二个和第三个分段索引时,相同的打印函数不打印任何内容。
当我将 IBAction 中的信息打印到控制台时,一切都正确更新。
为什么只有我的第一个索引使用 cellForRowAtIndexPath 方法注册?
最佳答案
我可能误导了您,让您相信我的问题出在我的逻辑中。我的问题是,在 numberOfRowsInSection 中,我没有考虑所有三个分段选项卡的情况,因此我的数据无法在 TableView 中正确显示。
谢谢@NoName
关于xcode - 分段选项卡 IBAction 不会传递给 cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35416294/