UITableView的scrollToRowAtIndexPath:atScrollPosition:animated的崩溃

本文介绍了UITableView中使用scrollToRowAtIndexPath:atScrollPosition:animated方法导致崩溃的问题及解决方案。当传入的IndexPath超出范围时,会导致崩溃。文章提供了代码示例来确保IndexPath的有效性,并建议在调用此方法前进行reloadData操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

UITableView的scrollToRowAtIndexPath:atScrollPosition:animated的崩溃

[摘要:reason: '-[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: row (0) beyond bounds (0) for section (0). 那个毛病是传进的IndexPath已越界了。须要正在挪用之前到场判别语句,没有影响机能的情]

 

 

reason: '-[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: row (0) beyond bounds (0) for section (0).


这个错误是传入的IndexPath已经越界了。需要在调用之前加入判断语句,不影响性能的情况下,在调用之前要先reloadDate

代码示例:

 

@implementation UITableView(ScrollToTopOrBottom)

-(void)scrollToTopWithAnimated: (BOOL)animated{
    if([selfnumberOfSections]>0&&[selfnumberOfRowsInSection: 0]>0){
        [selfscrollToRowAtIndexPath: [NSIndexPathindexPathForRow: 0inSection: 0]atScrollPosition: UITableViewScrollPositionTopanimated: animated];
    }
}

-(void)scrollToBottomWithAnimated: (BOOL)animated{
    if([selfnumberOfSections]>0){
        NSIntegerlastSectionIndex=[selfnumberOfSections]-1;
        NSIntegerlastRowIndex=[selfnumberOfRowsInSection: lastSectionIndex ]-1;
        if(lastRowIndex>0){
            NSIndexPath*lastIndexPath=[NSIndexPathindexPathForRow: lastRowIndexinSection: lastSectionIndex];
            [selfscrollToRowAtIndexPath: lastIndexPathatScrollPosition: UITableViewScrollPositionBottomanimated: animated];
        }
    }
}@end

 

 


转载于:https://www.cnblogs.com/frounk/p/5372183.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值