表格单选实现UITableViewCellAccessoryCheckmark



第一种方式

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    
    cell.textLabel.text = self.data[indexPath.row];

    if (self.indexPath == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    取出对应的主题名称
    NSString *themeName = self.data[indexPath.row];

    [ThemeManager shareManager].themeName = themeName;
    
//   取消选中单元格
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
//   如果点击的这行之前点击过一次 直接返回
//    if(indexPath.row==self.indexPath)  return;
    
//   1.取得点击的那一行的单元格(旧)
    UITableViewCell *oldCell =[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.indexPath inSection:0]];
//   隐藏对号
    oldCell.accessoryType = UITableViewCellAccessoryNone;
    
//   2.取得点击的那一行的单元格(新)
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    
//   显示对号
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;
    
//   将点击的行号记录到全局
    self.indexPath=indexPath.row;
    
//   当前点击的行号记录到本地
    [[NSUserDefaults standardUserDefaults] setValue:@(self.indexPath) forKey:@"indexPath"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}

- (instancetype)init
{
    if (self = [super init]) {
       
//        push后隐藏标签栏
       self.navigationController.hidesBottomBarWhenPushed = YES;
//        本地读取数据
       self.indexPath = [[[NSUserDefaults standardUserDefaults] objectForKey:@"indexPath"] floatValue];

        
        
    }
    return self;
}


第二种方式

//懒加载获取数据
- (NSArray *)data
{
    if (_data == nil) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"theme.plist" ofType:nil];
        
        NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
        
        _data = [dic allKeys];

//        每次从字典取值进行排序
        _data = [_data sortedArrayUsingSelector:@selector(compare:)];
    }
    
    return _data;
}


#pragma mark - 代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return  self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    
    cell.textLabel.text = self.data[indexPath.row];
    
    //如果是当前选中的主题,则给辅助图标
    NSString *themeName = [ThemeManager shareInstance].themeName;
    if ([cell.textLabel.text isEqualToString:themeName]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    取出对应的主题名称
    NSString *themeName = self.data[indexPath.row];
//    设置主题显示
    [ThemeManager shareInstance].themeName = themeName;

//   取消选中单元格
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
//    刷新数据
    [tableView reloadData];
    
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值