實現思路比較簡單,這里僅做記錄:
直接上代碼:
1,實現didSelectRowAtIndexPath方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [[NSUserDefaults standardUserDefaults]setValue:[array objectAtIndex:indexPath.row] forKey:APP_CHANGEVOICE]; [_sextTableView reloadData]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; }
在cellForRowAtIndexPath里面實現方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellTableIdentifier = @"CellTableIdentifier"; hPublickCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (cell == nil) { cell = [[hPublickCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellTableIdentifier]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } cell.textsLabel.text = array[indexPath.row]; cell.selectionStyle=UITableViewCellSelectionStyleGray; //選擇狀態的存儲 if ([[[NSUserDefaults standardUserDefaults]valueForKey:APP_CHANGEVOICE] isEqualToString:[array objectAtIndex:indexPath.row]]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; } return cell; }
這里面的array是數據源數組。效果圖如下:
2,上面這種是系統的選中樣式,下面是自定義的:
代碼如下:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ShippingAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShippingAddressCell"]; if (!cell) { cell = [[ShippingAddressCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ShippingAddressCell"]; } if (self.lastIndexPath == indexPath) { cell.selectedImg.image = [UIImage imageNamed:@"clicked"]; }else { cell.selectedImg.image = [UIImage imageNamed:@"unClick"]; } return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //之前選中的,取消選擇 ShippingAddressCell *celled = [tableView cellForRowAtIndexPath:_lastIndexPath]; celled.selectedImg.image = [UIImage imageNamed:@"unClick"]; //記錄當前選中的位置索引 _lastIndexPath = indexPath; //當前選擇的打勾 ShippingAddressCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.selectedImg.image = [UIImage imageNamed:@"clicked"]; }
這樣就可以實現了,截圖如下:
多選的有空再完善!代碼可以直接粘貼使用!
多選,這里我這個單選,沒有使用編輯模式,很簡單,在這里記錄下,demo就不附了,我把代碼都粘出來,
我的tableview是拖的xib,不想寫,代碼如下:
1,自定義cell,我是在最左邊有一個imageView,如下圖:
2,相關代碼:
a 聲明
@property (weak, nonatomic) IBOutlet UITableView *startTableView; @property (strong, nonatomic) NSMutableArray *selectIndexs; //多選選中的行
b viewDidLoad里面
_selectIndexs = [[NSMutableArray alloc]initWithCapacity:0]; for (int i = 0; i < dataAry.count; i ++) { [_selectIndexs addObject:@"0"]; }
c tableview的相關代理方法:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return dataAry.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { StartReachedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StartReachedCell"]; if (!cell) { cell = [[StartReachedCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StartReachedCell"]; } cell.descLab.text = dataAry[indexPath.row]; if ([_selectIndexs[indexPath.row] isEqualToString:@"0"]) { cell.selImg.image = [UIImage imageNamed:@"unsel"]; }else { cell.selImg.image = [UIImage imageNamed:@"sel"]; cell.tintColor = BLUECOLOR; } return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { StartReachedCell *cell = [tableView cellForRowAtIndexPath:indexPath]; NSString *stateStr = _selectIndexs[indexPath.row]; //連續點擊的時候,兩種狀態進行切換 NSString *picName = [stateStr isEqualToString:@"0"]? @"sel":@"unsel"; NSString *changeStateStr = [stateStr isEqualToString:@"0"]?@"1":@"0"; cell.selImg.image = [UIImage imageNamed:picName]; [_selectIndexs replaceObjectAtIndex:indexPath.row withObject:changeStateStr]; }
效果如下圖:
基本的邏輯是,聲明一個空的數組,里面存放我自定義的狀態:"0"和"1",第一次進去的時候,是沒有狀態1的,只有0,在cell的初始化方法里面直接判斷展示即可,在進行過點擊操作后,在didSelectRowAtIndexPath方法里面把選中所產生的新狀態替換掉原來數組里面的老狀態,一次循環展示即可。如果想要下次進去是上一次默認的選中,直接把數組存起來即可。
網上有很多的編輯狀態,確實很好用,也很簡單,有空我會更新下,把單選、復選、多選,全部都更新出來,我這種短小精悍。大家有什么好的寫法,可以留言!
默認選中:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.tableView selectRowAtIndexPath:self.lastIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop]; NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:0]; [self tableView:self.tableView didSelectRowAtIndexPath:path]; }