循环数组和重新加载数据的更好方法?

时间:2015-08-13 21:53:40

标签: ios swift uitableview for-loop parse-platform

我试图只显示符合条件的项目(如果位置< = searchingDistance),但似乎这是显示此数据的效率非常低的方式。在检查每个位置后,此函数是否重新加载整个tableview?如果是这样,这不是对记忆/时间的可怕使用吗?

什么是更好的选择?

var locations = [Int: Int]()



func searchSuccessful() {

var row: Int = 0
var section: Int = 0

for (index, location) in locations {

if location <= searchedDistance {

    self.theTableView.beginUpdates()
    let indexPath = NSIndexPath(forRow: row, inSection: section)
    self.theTableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    self.theTableView.endUpdates()

        }
    }
}

1 个答案:

答案 0 :(得分:0)

将开始/结束更新放在循环之外。

self.theTableView.beginUpdates()

// Do all the changes in a single place, surrounded by begin/end updates
for (index, location) in locations {
    if location <= searchedDistance {
        let indexPath = NSIndexPath(forRow: row, inSection: section)
        self.theTableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    }
}
self.theTableView.endUpdates()

<强>夫特

self.tableView.beginUpdates()
let indexPath = NSIndexPath(forRow: row, inSection: section)
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
self.tableView.endUpdates()

<强>的OBJ-C

[self.tableView beginUpdates];
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];