ios - 为什么模拟器只有12行?

标签 ios swift uitableview

我的数据有 50 个条目。模拟器12点就停了,不懂。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    funcSectCount += 1
    let data = AppData.orgNames
    print("data.count=", data.count, "  func Section count=", funcSectCount)
    return data.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    funcRowCount += 1
    let data = AppData.orgNames
    let cell = myTable.dequeueReusableCell(withIdentifier: "myCell", for: 
    indexPath)
    cell.textLabel?.text = data[indexPath.row]
    print("indexPath.row=", indexPath.row, "  func Row count=", funcRowCount)
    return cell
}

这是每个函数的最终打印:

data.count= 50 func Section count= 3 (curious, but minor)
indexPath.row= 11 func Row count= 12

最佳答案

tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 每次需要一个新单元格时由 TableView 调用。

如果一次只有 12 个单元格可见,那么 TableView 最初只需要 12 个单元格,因此只会要求 12 个单元格。您必须滚动才能需要更多。在需要单元格之前,它不会请求单元格。

关于ios - 为什么模拟器只有12行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43882496/

相关文章:

ios - 无法使以编程方式创建的 UITextField 可编辑 (iOS)

swift - “ fatal error :在展开可选值时意外发现nil”是什么意思?

ios - 以编程方式将 UIView 添加到 UITableView 的底部

ios - 快速异步加载tableView图像

iphone - 如何在 iPhone 的 TableCell 中获取 TextField 的位置?

ios - 使用 mergeChangesFromContextDidSaveNotification 时 CoreData 无法完成错误

ios - 获取一串单词和空格中的第一个单词 - 空格前的子串第一个单词

ios - "defer"是否保证在 Swift 中被调用?

ios - 在自定义 UITextField 类中实现 TextField 协议(protocol)

ios - 在快速生成警报时,我是否必须对 "OK"按钮进行硬编码/本地化?