修改tableViewCell的默認checkmark顏色 並實現其單選功能

藍色是tableViewCell的默認前景色(tintColor),所以我們設置cell.tintColor = [UIColor redColor];就可以改前景色爲紅色了,同樣那個accessoryCheckmark的顏色就變成紅色了。 

首先我們公開一個屬性 

@property(nonatomic,strong)NSIndexPath *lastPath;

主要是用來接收用戶上一次所選的cell的indexpath



在cellForRowAtIndexPath中加入

-

    if (self.selected == indexPath.row) {

        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        

    }

    else

        cell.accessoryType = UITableViewCellAccessoryNone;

    

    return cell;



隨後添加action

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.selected inSection:0]];

    selectedCell.accessoryType = UITableViewCellAccessoryNone;

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {

        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        self.selected = indexPath.row;

    }

    else

    {

        cell.accessoryType = UITableViewCellAccessoryNone;

    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}


如需自定義checkmark的圖片 設置cell.accessoryView爲自己所需的UIImageView即可


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章