ios - 在不同部分的索引处选择行

标签 ios swift didselectrowatindexpath

我有一个包含两个部分的表格。我可以在索引处选择行 - 但我无法区分各个部分。

该表是一个拉出菜单 - 仅在代码中创建。

选择的委托(delegate)方法是

func sideBarControlDidSelectRow(indexPath: NSIndexPath) {
    delegate?.sideBarDidSelectButtonAtIndex(indexPath.row)
    sectionis = indexPath.section
    NSLog("Index Path Section %d", indexPath.section)
}

这给了我控制台中的部分。

但是在 View Controller 上我只有

func sideBarDidSelectButtonAtIndex(index: Int){
    switch(index) {
        etc.
    }
}

我需要做的是类似

func sideBarDidSelectButtonAtIndex(index: Int){
    if section == 0 {
        switch(index){
            etc.
        }
    }
}

但如果我尝试这样做,则二元运算符不能应用于类型部分的操作数

这一定是显而易见的事情,因为表格一直都有部分 - 但答案却在躲避我。

最佳答案

只需更新委托(delegate)协议(protocol)中方法 sideBarDidSelectButtonAtIndex 的声明以接受 NSIndexPath 值,更改:

func sideBarDidSelectButtonAtIndex(index: Int)

func sideBarDidSelectButtonAtIndex(indexPath: NSIndexPath)

那么你可以去掉这个方法

func sideBarControlDidSelectRow(indexPath: NSIndexPath) {
    delegate?.sideBarDidSelectButtonAtIndex(indexPath.row)
}

并用

替换它的调用
delegate?.sideBarDidSelectButtonAtIndex(indexPath)

然后你将收到完整的 indexPath 并可以做你想做的事

关于ios - 在不同部分的索引处选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30834882/

相关文章:

ios - Storyboard segue 在 UITableView 的 didSelectRow 之前被调用

ios - 同一服务器的不同 REST 端点之间的 URLSession

ios - 下一个轨道AVPlayer如何

ios - 从在线数据库填充 UICollectionView

ios - 我怎样才能让真正的苹果 watch 打开父应用程序

objective-c - 当我点击 didSelectRowAtIndexPath 时,UISearchBar 中的自动更正会干扰

ios - 在 UITableView 中获取输入 onclick

javascript - 动态替换ios项目中的.h和.m文件(bundle路径)

ios - 在 iOS 上后台运行应用程序

ios - viewWillAppear 在 cellForItemAtIndexPath 之后调用?