項目裏有一個需求,選中的收貨地址和其他的收貨地址樣式不同
選中之後,要動態的改變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