ios - TableViewController numberOfRowsInSection 部分

标签 ios swift

我有一个带按钮的 Controller 和一个带 10 个数组的 TableViewController。每个按钮都有一个索引,该索引传递给 TableViewController

TableViewController 中,代码如下所示:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if buttonIndex == 0 {
        return array0.count
    }
    else if buttonIndex == 1 {
        return array1.count
    }
    else if buttonIndex == 2 {
        return array2.count
    }
    else if buttonIndex == 3 {
        return array3.count
    }
    else if buttonIndex == 4 {
        return array4.count
    }
    else if buttonIndex == 5 {
        return array5.count
    }
    else if buttonIndex == 6 {
        return array6.count
    }
    else if buttonIndex == 7 {
        return array6.count
    }
    else if buttonIndex == 8 {
        return array6.count
    }

    return array0.count
}

我想自动定义当前索引来执行此操作:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //return array(currentIndex).count
    }

怎么做?

最佳答案

实现您想要的目标的一种方法是创建一个映射,其中索引作为键,数组作为值。

所以你的代码应该是这样的:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return indicesArray[index].count
}

关于ios - TableViewController numberOfRowsInSection 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45752164/

相关文章:

iphone - 来自字符串的 CGPathRef

ios - 将 (Int, String) 转换为 String 以打印数组

ios - 使用 IBAction 将数据传递到另一个 View Controller 而不使用 segue swift

ios - fatal error : unexpectedly found nil while unwrapping an Optional value Parse sign up

ios - 授权给 HealthKit 时,错误 '(_, _) -> Void' 无法转换为 'HealthManager'

ios - 删除 UISearchBar 的上下边框和阴影?

ios - 无法打开 NSError

iphone - 改变 super View 的不透明度奇怪的效果导致 subview 奇怪的不透明度变化

iphone - openEars 项目错误和警告

swift - 在 Swift 4 更新后获取用户的位置不起作用