UITableView的代理方法didDeselectRowAtIndexPath不响应

本文探讨UITableView动画隐藏过程中代理方法didDeselectRowAtIndexPath未被触发的问题。通过实例代码展示了如何使用标记属性解决该问题,实现选中行动画隐藏及取消前次选中状态。

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

偶然遇到UITableView的代理方法didDeselectRowAtIndexPath不响应。


项目中要实现选中表格某一行时,动画隐藏UITableView,同时取消选中上一次选中的行,即如下效果。

                 



最初代码是这样写的:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

CountryCell* cell = [tableView cellForRowAtIndexPath:indexPath];

        cell.confirmImgView.hidden = NO;


//动画隐藏UITableView

[UIView animateWithDuration:0.3 animations:^{

        CGRect frame = _tableView.frame;

        frame.origin.y = Screen_Height;

        _tableView.frame = frame;

    }];

}


- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

    CountryCell* cell = [tableView cellForRowAtIndexPath:indexPath];

    cell.confirmImgView.hidden = YES;

}


结果却没有到达预期效果,didDeselectRowAtIndexPath代理方法根本就没有触发,如下图:




无意中发现注释掉动画隐藏UITableView那段代码,就会触发didDeselectRowAtIndexPath代理方法,不知道为什么会这样。

只能用一种比较笨的方法实现:

加一个标记属性,初始值为-1: 


_selRow = -1;


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


//找到上一次选中的行

if (_selRow >= 0) {

        CountryCell* selCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:

_selRow inSection:0]];

                selCell.confirmImgView.hidden = YES; //didDeselectRowAtIndexPath方法就不需要了

        }


        CountryCell* cell = [tableView cellForRowAtIndexPath:indexPath];

        cell.confirmImgView.hidden = NO;

_selRow = indexPath.row;

//动画隐藏UITableView

[UIView animateWithDuration:0.3 animations:^{

        CGRect frame = _tableView.frame;

        frame.origin.y = Screen_Height;

        _tableView.frame = frame;

    }];

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值