IOS -- UITableView里面点击一个cell改变其他cell的设置

CustomTableViewCell.h

#import <UIKit/UIKit.h>

@interface CustomTableViewCell : UITableViewCell
@property (nonatomic, strong) IBOutlet UIImageView *taskUIimage;
@property (nonatomic, strong) IBOutlet UILabel *taskName;
@property (nonatomic, assign) BOOL isSelected;

@end

CustomTableViewCell.m

#import "CustomTableViewCell.h"

@implementation CustomTableViewCell
-(void)setSelected:(BOOL)selected animated:(BOOL)animated{
  
    [self setSelected:selected];
    [super setSelected:selected animated:animated];
    
    
}
-(void)setSelected:(BOOL)isSelected{
    _isSelected = isSelected;
    if (_isSelected) {
        _taskName.text= @"YES";
    }
    else{
        _taskName.text= @"NO";
    }
}
@end

ViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *reuseIdentifier=@"reuseIdentifier";
    CustomTableViewCell *cell=nil;
    cell=[tableView dequeueReusableCellWithIdentifier:reuseIdentifier forIndexPath:indexPath];
    if(cell==nil)
    {
        cell=[[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    cell.taskUIimage.image=[UIImage imageNamed:@"chongwu1019.jpg"];
    cell.isSelected = NO;
    //默认选中第五行
    if (indexPath.row == 4) {
        [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    }
    
    return cell;
    
}

 

posted on 2017-10-09 17:27  麦芽呀~  阅读(1240)  评论(0编辑  收藏  举报