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; }