美文网首页
UITableViewCell选中对勾

UITableViewCell选中对勾

作者: frola_ | 来源:发表于2017-09-24 10:05 被阅读0次

    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选中对勾

          本文链接:https://www.haomeiwen.com/subject/xvzcextx.html