xcode - 分段选项卡 IBAction 不会传递给 cellForRowAtIndexPath

标签 xcode swift uitableview uisegmentedcontrol

我的问题是我的分段控件不会将信息传递给我的 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/

相关文章:

ios - 在 UICollection 数据源读取响应之前,如何处理来自 API 的响应?

swift - iOS 13 - UIMenu 是否存在不显示其图像的错误?

使用 Xcode 11 构建后,iOS 应用程序速度慢了 20 倍以上

xcode - 在 Xcode 编辑器中看不到 3D 模型文件

swift - 如何在 swift 中简单地连接到 NSString

iphone - 寻找关于将 Pre-Storyboard 代码 (XCode4) 移动到 Storyboard 代码 (XCode5) 的教程

swift - Tableview 单元格阴影无法正确呈现 Swift

objective-c - 使用 NSData 在 NSTextView 中设置文本

ios - 为什么 Xcode 以不同的方式对待两个相似的变量声明,其中第一个只显示警告,第二个显示警告 + 错误?

objective-c - 转义闭包是如何在 Swift 3 中实现的(底层)?它们是否像在 objective-c 中那样隐式地 block_copied/retained?