UITableViewCell选中对勾

accessoryType的颜色可以通过修改cell的tintColor表层颜色改变

cell.tintColor = [UIColor redColor];

默认点击第一行

    NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:0];
    [self tableView:__mainTableView didSelectRowAtIndexPath:path];

实现代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  static NSString *cellIdentifier = @"cellIdentifier";
    
    UITableViewCell *cell = [__mainTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    //NSIndexPath *path=[NSIndexPath indexPathForRow:0 inSection:0];
//默认点击第一行
    NSIndexPath *path = [NSIndexPath indexPathForItem:0 inSection:0];
    [self tableView:__mainTableView didSelectRowAtIndexPath:path];
    
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"UITableViewCell" owner:nil options:nil]firstObject];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
        NSInteger row = [indexPath row];
        NSInteger oldRow = [_lastPath row];
        
        if (row == oldRow && _lastPath!=nil) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            
            cell.tintColor = [UIColor redColor];
            
            
        }else{
            
            cell.accessoryType = UITableViewCellAccessoryNone;
            
        }
         
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section== 0) {
        NSInteger newRow = [indexPath row];
        
        NSInteger oldRow = (self .lastPath !=nil)?[self .lastPath row]:-1;
        if (newRow != oldRow) {
            
            mySettingTableViewCell *newCell = [__mainTableView cellForRowAtIndexPath:indexPath];
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;
            newCell.tintColor = [UIColor redColor];
            mySettingTableViewCell *oldCell = [__mainTableView cellForRowAtIndexPath:_lastPath];
            
            oldCell.accessoryType = UITableViewCellAccessoryNone;
            
            self .lastPath = indexPath;
        }
        
        [__mainTableView deselectRowAtIndexPath:indexPath animated:YES];
        
    }
    
}

你可能感兴趣的:(UITableViewCell选中对勾)