实现思路比较简单,这里仅做记录:
直接上代码:
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"]; }
这样就可以实现了,截图如下:
多选的有空再完善!代码可以直接粘贴使用!
原文地址:https://www.cnblogs.com/hero11223/p/8601038.html
时间: 2024-10-10 04:31:47