Che*_*ong 4 uitableview ios swift
有时当我试图将tableview滚动到一行时,我可能会意外地提供一个不存在的部分/行.然后应用程序将崩溃.
self.tableView.scrollToRow(at: IndexPath(row: targetRow, section: targetSection), at: UITableViewScrollPosition.bottom, animated: true);
Run Code Online (Sandbox Code Playgroud)
如何使这个滚动过程崩溃安全?我的意思是,如果我提供不存在的部分/行,我希望UITableView只是忽略它.或者如何在滚动之前检查UITableView中是否存在节/行?谢谢.
Raj*_*r R 16
使用这个 UITableView 扩展来检查 Indexpath 是否有效。
extension UITableView
{
func indexPathExists(indexPath:IndexPath) -> Bool {
if indexPath.section >= self.numberOfSections {
return false
}
if indexPath.row >= self.numberOfRows(inSection: indexPath.section) {
return false
}
return true
}
}
Run Code Online (Sandbox Code Playgroud)
像这样使用
var targetRowIndexPath = IndexPath(row: 0, section: 0)
if table.indexPathExists(indexPath: targetRowIndexPath)
{
table.scrollToRow(at: targetRowIndexPath, at: .bottom, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
试试这个 -
let indexPath = IndexPath(row: targetRow, section: targetSection)
if let _ = self.tableView.cellForRow(at: indexPath) {
self.tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.bottom, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
756 次 |
| 最近记录: |