因為我們的App有商品兌換這一機制,故我們必須要有一個郵寄地址管理的頁面,就在剛才自己簡單的寫了下實現(xiàn)單選和復(fù)選的問題,其實有兩種方法,這里先給大家說下其中一種方法。
在我們做選擇時,必須在每次點擊時記錄cell的indexPath;
下面給大家看下我的實現(xiàn)方法:
單選:
先定義一個變量,記錄每次點擊的cell:
@property (assign, nonatomic) NSIndexPath *selIndex;//單選,當(dāng)前選中的行
接著在tableView的點擊代理方法中實現(xiàn)代碼
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
AddressTableViewCell *celld = [tableView cellForRowAtIndexPath:_selIndex];
celld.select_Ima.image = [UIImage imageNamed:@"未選"];
celld.givenAddress.textColor = GZ_RGB(102, 102, 102);
//記錄當(dāng)前選中的位置索引
_selIndex = indexPath;
//當(dāng)前選擇的打勾
AddressTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.select_Ima.image = [UIImage imageNamed:@"sele"];
cell.givenAddress.textColor = GZ_RGB(227, 47, 73);
[self.AddressTableView reloadData];
}
最后,一般來說在地址管理中會有多個地址,因此我們必須要考慮到cell復(fù)用的問題,所以我們需要判斷
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
AddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Address"];
if (!cell) {
cell = [[AddressTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Address"];
}
__weak typeof(self) weakSelf = self;
__block AddressTableViewCell * weakCell = cell;
[cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
cell.delegate = self ;
cell.selectionStyle = GZTableViewCellStyle ;
cell.name.text = [NSString stringWithFormat:@"輕斟淺醉17%ld",(long)indexPath.row];
cell.phone.text = @"12345678987";
cell.address.text = @"陜西省西安市高新區(qū)坦言公館1433";
if (_selIndex == 0 && indexPath.row == 0) {
weakCell.select_Ima.image = [UIImage imageNamed:@"sele"];
weakCell.givenAddress.textColor = GZ_RGB(227, 47, 73);
}
//當(dāng)上下滑動時因為cell復(fù)用,需要判斷哪個選擇了
else if (_selIndex == indexPath) {
weakCell.select_Ima.image = [UIImage imageNamed:@"sele"];
weakCell.givenAddress.textColor = GZ_RGB(227, 47, 73);
}else{
weakCell.select_Ima.image = [UIImage imageNamed:@"未選"];
weakCell.givenAddress.textColor = GZ_RGB(102, 102, 102);
}
cell.AddressCellMoreButtonBlock = ^(NSInteger tag){
if(tag == 1111){
NewAddressVC *new = [[NewAddressVC alloc]init];
[self.navigationController pushViewController:new animated:YES];
}else{
[weakSelf.view gz_showHUD:@"刪除地址" hide:1];
}
};
return cell ;
}
多選:
先定義一個數(shù)組,記錄每次點擊的cell:
@property (assign, nonatomic) NSMutableArray *selIndexs;//多選,當(dāng)前選中的行
初始化一下數(shù)組
_selIndexs = [NSMutableArray new];
接著在代理方法中實現(xiàn)代碼
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//獲取到點擊的cell
AddressTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { //如果為選中狀態(tài)
cell.accessoryType = UITableViewCellAccessoryNone; //切換為未選中
[_selIndexs removeObject:indexPath]; //數(shù)據(jù)移除
}else { //未選中
cell.accessoryType = UITableViewCellAccessoryCheckmark; //切換為選中
[_selIndexs addObject:indexPath]; //添加索引數(shù)據(jù)到數(shù)組
}
}
最后再數(shù)據(jù)方法中處理
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
AddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Address"];
if (!cell) {
cell = [[AddressTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Address"];
}
__weak typeof(self) weakSelf = self;
__block AddressTableViewCell * weakCell = cell;
[cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
cell.delegate = self ;
cell.selectionStyle = GZTableViewCellStyle ;
cell.name.text = [NSString stringWithFormat:@"輕斟淺醉17%ld",(long)indexPath.row];
cell.phone.text = @"12345678987";
cell.address.text = @"陜西省西安市高新區(qū)坦言公館1433";
cell.accessoryType = UITableViewCellAccessoryNone;
for( NSIndexPath *index in _selIndexs){
if(index == indexPath)
cell.select_Ima.image = [UIImage imageNamed:@"sele"];
cell.givenAddress.textColor = GZ_RGB(227, 47, 73);
}
cell.AddressCellMoreButtonBlock = ^(NSInteger tag){
if(tag == 1111){
NewAddressVC *new = [[NewAddressVC alloc]init];
[self.navigationController pushViewController:new animated:YES];
}else{
[weakSelf.view gz_showHUD:@"刪除地址" hide:1];
}
};
return cell ;
}
幫助到了小伙伴可以點個贊,我這里也做了地址編輯和刪除功能,如果有需要的可以聯(lián)系我QQ群號:237573715 歡迎您的加入。