点击选中cell后改变cell的样式

来源:互联网 发布:php当中的 k 编辑:程序博客网 时间:2024/05/01 14:30

项目里有一个需求,选中的收货地址和其他的收货地址样式不同
这里写图片描述

选中之后,要动态的改变cell的样式,并在返回上一个控制器的时候更新显示,
这里写图片描述

上一个控制器需要更改的显示,如下图所示:

这里写图片描述

之前想通过点击cell触发cell的-(void)layoutSubviews函数,进行子控件frame的改变,没被选中的cell,打钩的UIImageView的frame设置为0,被选中的设置为相应的正常尺寸,其他的控件的frame依次进行改变。后来还是觉得直接加载两种cell比较方便。因此绘制了两种不同的cell,一种是被选中的,一种是为被选中的。

设置一个标志位

@property (nonatomic,assign) int indexSlected;

这个标志位记录被选中的cell的indexPath.row,每次被选中之后,就改变标志位的值,并且在UITableView reloadData的时候,加载不同的cell.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.row == _indexSlected) {        GCSlectedAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID2"];        cell.model = _addressArrs[indexPath.row];        cell.selectionStyle = UITableViewCellSelectionStyleNone;        return cell;    }else{        GCAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID1"];        cell.model = _addressArrs[indexPath.row];        cell.selectionStyle = UITableViewCellSelectionStyleNone;        return cell;    }}

选中之后还要更新上一个控制器的页面。我选择使用通知的方式,在点击cell的时候就发出通知,附带上对应的标志位的数值,上一个控制器收到之后,就从模型数组中,加载对应的model。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    _indexSlected = (int)indexPath.row;    [_mainTableView reloadData];    NSDictionary *info = @{@"info":[[NSString alloc]initWithFormat:@"%d",_indexSlected]};    [[NSNotificationCenter defaultCenter]postNotificationName:@"selectAddress" object:nil userInfo:info];}

上一个控制器接收到通知后:

#pragma mark -接收到消息,更新地址-(void)renewSelecteAddress:(NSNotification *)notification{    NSString *seletedIndexStr = notification.userInfo[@"info"];    _indexSelectedAddress = seletedIndexStr.intValue;    [_mainTableView reloadData];}

注意上一个控制器中也有一个标志位_indexSelectedAddress

0 0