【问题标题】:selectrow at index for different sections不同部分的索引处的选择行
【发布时间】:2015-06-14 21:45:06
【问题描述】:

我有一张有两个部分的桌子。我可以在索引处选择行 - 但我无法区分各个部分。

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

选择的委托方法是

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

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

但在视图控制器上我只有

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

我需要做的就是像

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

但是如果我尝试这样做,那么二元运算符就不能应用于 section 类型的操作数

这必须是显而易见的,因为表格总是有部分 - 但答案是我一直没有找到。

【问题讨论】:

    标签: ios swift didselectrowatindexpath


    【解决方案1】:

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

    func sideBarDidSelectButtonAtIndex(index: Int)
    

    func sideBarDidSelectButtonAtIndex(indexPath: NSIndexPath)
    

    那么你可以删除这个方法

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

    并将其调用替换为

    delegate?.sideBarDidSelectButtonAtIndex(indexPath)
    

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

    【讨论】:

    • 知道怎么做就这么简单。
    猜你喜欢
    • 2020-10-27
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    • 2021-05-12
    相关资源
    最近更新 更多