简体   繁体   English

如何解决 uiTableview 滚动应用程序崩溃,它在 swift 中引发数组索引超出范围异常?

[英]How do I solve the uiTableview scrolling app crash which throws array index out of bound exception in swift?

I am loading data and updating the cell, when I scroll quickly the app crashes and throws array index out of bound at cellforrowat: index path.我正在加载数据并更新单元格,当我快速滚动时,应用程序崩溃并在 cellforrowat: 索引路径处抛出数组索引超出范围。 How do I overcome this problem?我该如何克服这个问题?

Your tableView is trying to access data from your data source which actually does not exist.您的 tableView 正在尝试从您的数据源访问实际上不存在的数据。

Assuming you have a data source like this.假设您有这样的数据源。

let dataSource = [[String]]()

Then your table view delegate methods should look like this.然后你的表视图委托方法应该是这样的。

func numberOfSections(in tableView: UITableView) -> Int {
     return dataSource.count
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let data = dataSource[indexPath.section][indexPath.row]
     
   //your code     
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM