zoukankan      html  css  js  c++  java
  • (七十一)关于UITableView退出崩溃的问题和滚动究竟部的方法

    【TableView退出崩溃的问题】

    近期在使用TableView时偶然发如今TableView中数据较多时,假设在滚动过程中退出TableView到上一界面。会引起程序的崩溃。经过网上查阅和思考我发现这样的情况出如今一个UIView控制器拥有一个TableView,TableView无法在UIView销毁前全然销毁,从而继续调用dataSource,而这时候UIView已经不可用了,会引发野指针错误。

    避免方法非常easy,仅仅须要在UIView的dealloc方法中把dataSource设为nil就可以:

    - (void)dealloc{
        
        self.tableView.dataSource = nil;
        
    }

    【TableView滚动究竟部】

    对于即时聊天等应用,经常须要在新数据到来时把TableView滚动究竟部。这个需求能够通过TableView的scrollToRowAtIndexPath::实现,须要传入要滚动到的cell位置和滚动位置。

    须要传入indexPath的最后一个位置,也就是要显示的数据数组的最后一个元素的索引,位置为底部。枚举名为

    UITableViewScrollPositionBottom。例如以下:

    Tip:一定要注意在没有数据时会造成indexPath.row=-1,此时应当直接返回。

    - (void)scrollToTableBottom{
        
        if (_array.count < 1) {
            return;
        }
        NSInteger lastRow = _array.count - 1;
        NSIndexPath *lastPath = [NSIndexPath indexPathForRow:lastRow inSection:0];
        [self.tableView scrollToRowAtIndexPath:lastPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        
    }


  • 相关阅读:
    centos 用户管理
    rsync 实验
    文件共享和传输
    PAT 1109 Group Photo
    PAT 1108 Finding Average
    PAT 1107 Social Clusters
    PAT 1106 Lowest Price in Supply Chain
    PAT 1105 Spiral Matrix
    PAT 1104 Sum of Number Segments
    PAT 1103 Integer Factorization
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7003071.html
Copyright © 2011-2022 走看看