iOS选中cell改变cell状态,同时改变其他cell状态

CourseListTableViewCell.h

#import 

NS_ASSUME_NONNULL_BEGIN

@interface CourseListTableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *lLabel;

@property (nonatomic, assign) BOOL isSelected;
@end

NS_ASSUME_NONNULL_END

CourseListTableViewCell.m

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [self setSelected:selected];
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setSelected:(BOOL)isSelected{
    _isSelected = isSelected;
    
    if (_isSelected) {
        self.lLabel.textColor = l_ColorFromRGB(0xff8800);  //这个是改变了文本的颜色
    }
    else{
        self.lLabel.textColor = l_ColorFromRGB(0x333333);  //改变了文本的颜色
    }
}

其他不需要怎么改变。如果需要有默认值的话,可以- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中实现

    NSString *reuseIdentifier=@"reuseIdentifier";
    ....
    //默认选中第五行
    if (indexPath.row == 4) {
        [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    }
    
    return cell;

你可能感兴趣的:(iOS选中cell改变cell状态,同时改变其他cell状态)