因?yàn)槲覀兊腁pp有商品兌換這一機(jī)制秽荤,故我們必須要有一個(gè)郵寄地址管理的頁(yè)面爬早,就在剛才自己簡(jiǎn)單的寫(xiě)了下實(shí)現(xiàn)單選和復(fù)選的問(wèn)題捍掺,其實(shí)有兩種方法,這里先給大家說(shuō)下其中一種方法靶擦。
在我們做選擇時(shí)腮考,必須在每次點(diǎn)擊時(shí)記錄cell的indexPath;
下面給大家看下我的實(shí)現(xiàn)方法:
單選:
先定義一個(gè)變量玄捕,記錄每次點(diǎn)擊的cell:
@property (assign, nonatomic) NSIndexPath *selIndex;//單選踩蔚,當(dāng)前選中的行
接著在tableView的點(diǎn)擊代理方法中實(shí)現(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];
}
最后,一般來(lái)說(shuō)在地址管理中會(huì)有多個(gè)地址枚粘,因此我們必須要考慮到cell復(fù)用的問(wèn)題馅闽,所以我們需要判斷
-(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)上下滑動(dòng)時(shí)因?yàn)閏ell復(fù)用,需要判斷哪個(gè)選擇了
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 ;
}
多選:
先定義一個(gè)數(shù)組馍迄,記錄每次點(diǎn)擊的cell:
@property (assign, nonatomic) NSMutableArray *selIndexs;//多選福也,當(dāng)前選中的行
初始化一下數(shù)組
_selIndexs = [NSMutableArray new];
接著在代理方法中實(shí)現(xiàn)代碼
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//獲取到點(diǎn)擊的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 ;
}
幫助到了小伙伴可以點(diǎn)個(gè)贊,我這里也做了地址編輯和刪除功能攀圈,如果有需要的可以聯(lián)系我QQ群號(hào):237573715 歡迎您的加入暴凑。