arrays - 如何显示数组中的特定元素?

标签 arrays swift xcode

例如, 我有一个数组,所有元素都是 40。当我单击名为 aButton 的按钮时,我想将元素从 0 带到 9。当我点击 mButton 时,它会将元素从 10 带到 19。

 let arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28",   "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39"]

@IBAction func aButton(_ sender: Any) {
}

@IBAction func mButton(_ sender: Any) {
}

`

 @IBAction func aButton(_ sender: Any) {
    displayedElements = numberOfRows.filter {(0...9).contains(Int($0)!)}
}

@IBAction func mButton(_ sender: Any) {
    displayedElements = numberOfRows.filter {(10...19).contains(Int($0)!)}
}

var displayedElements:[String] = [] {
    didSet {
        tableview.reloadData()
    }
}


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

}

我看到此错误:无法将类型“[String]”的返回表达式转换为返回类型“Int”

最佳答案

您正在执行的过滤看起来应该可以工作(尽管我没有测试过),但是您的数据源需要一些工作。 numberOfRows 函数应如下所示:-

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return displayedElements.count
}

我现在假设您的 cellForRowAt 函数正确取消引用 displayedElements[indexPath.row] 以显示行。

关于arrays - 如何显示数组中的特定元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59277624/

相关文章:

xcode - 为什么我的 scrollView 是零?

ios - iOS swift 中 UIDocumentPickerController 的 Doc 文件问题?

javascript - 组合两个普通数组以创建 key :value pair array

ruby - 根据元素的索引连接数组元素

ios - 确定 UIStackView 内容大小

ios - 应用程序在自定义相机期间无声崩溃 (Swift)

ios - React Native iOS Release 构建停留在旧代码上,但 Debug 构建工作正常

iphone - 如何更改您的 Xcode iTunes Connect 登录名?

javascript - 操作对象数组时 For 循环未按预期工作

php - 查找数组是否包含元素