ios Cell中的按鈕實現單選

首先定義一個變量來記錄按鈕的狀態

@property (assign, nonatomic) NSIndexPath *selIndex;//單選,當前選中的行

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

  NSInteger row= [indexPath row];

    NSInteger oldRow = [_selIndex row];

    if (row == oldRow && _selIndex) {

        [cell.descriptionLabel setTitleColor:SubjectColor forState:UIControlStateNormal];

    }

}

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

  NSInteger newRow = [indexPath row];

    NSInteger oldRow = self.selIndex?[self.selIndex row]: - 1;

    if (newRow != oldRow) {

        MissTableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];

        [newCell.descriptionLabel setTitleColor:SubjectColor forState:UIControlStateNormal];

        MissTableViewCell *oldCell = [tableView cellForRowAtIndexPath:indexPath];

        [oldCell.descriptionLabel setTitleColor:RGB(152, 162, 177) forState:UIControlStateNormal];

        self.selIndex = indexPath;

    }

}

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