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];
        
    }


  • 相关阅读:
    接口,抽象类,普通类
    将svn项目导出,再导入到其他工作空间
    Ajax 与 Struts 1
    save tran tranName
    hibernate缓存机制详细分析
    sql中的group by 和 having 用法解析
    TensorBoard 实践 1
    Tensorflow 解决MNIST问题的重构程序
    在MNIST数据集,实现多个功能的tensorflow程序
    Tensorflow中的滑动平均模型
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7003071.html
Copyright © 2011-2022 走看看