例如, 我有一个数组,所有元素都是 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/