美文网首页
tableView的cell复用优化

tableView的cell复用优化

作者: 上帝死了众神在堕落 | 来源:发表于2017-03-15 15:39 被阅读0次

    UITableView是iOS中重要的控件,UITableView继承自UIScrollView,支持垂直滚动,而且性能极好,UITableViewCell可以复用。
    cell的重用

    static NSString *reuseID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID]; 
    if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseID];
     }
    

    1、给cell做个标记,当cell出屏幕(不用之后)把cell对象,存储在特定标示的缓存池,当tableView加载下一个数据的时候,从缓存池中取出 空闲的标记cell。
    2、如果缓存池中有空余的cell,会重新配置这个cell数据。给tableView显示。如果没有空余的cell,就重新创建。
    在iOS开发中,如果tableViewCell是变化的,复用cell会引起的视图重叠。
    解决方法:

    for(UIView *view in [cell subviews]){
        [view removeFromSuperview];
     }
    或
    static NSString *reuseID = @"cell";
    UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
    

    相关文章

      网友评论

          本文标题:tableView的cell复用优化

          本文链接:https://www.haomeiwen.com/subject/khnlnttx.html