自定義tableViewCell選中樣式,并默認選中

2017912205527dama.png

自定義選中樣式很簡單,直接在下面兩個方法中添加就行

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MLPayStyleCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selectButton.selected = YES;
    //傳值
    if (cell.model)
    {
        cell.model = _payStyleDataArr[indexPath.row];
        self.sendPayStyleValueBlock(cell.model.termValue);
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MLPayStyleCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selectButton.selected = NO;
}

設置默認選中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MLPayStyleCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MLPayStyleCell class]) forIndexPath:indexPath];
    
    cell.model = _payStyleDataArr[indexPath.row];
    
    if (indexPath.row == 0)
    {
        cell.selectButton.selected = YES;
//        cell.selected = YES;
        //設置cell的選中狀態,然后選中其他的cell時,可以調用didDeselectRowAtIndexPath:方法,取消cell上按鈕的選中狀態
        [self selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    }
    return cell;
}

如果利用cell.selected = YES來設置選中按鈕,會出現下面的情況

2017912205601dama.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容