iOS开发中自带的tableviewcell的选中效果是蓝色的对号,虽然可以修改颜色,但是不能修改图片挺无奈的,下图是我们UI给的图
直接上代码:
自定义cell的.h文件
#import <UIKit/UIKit.h>
@class ProjectModel;
@interface RoomSwitchTableViewCell : UITableViewCell
/** 房间信息*/
@property (nonatomic,strong) ProjectModel *roomModel;//传的数据
@property (nonatomic,strong) UIImageView *roomImageV;
@end
自定义cell的.m文件
#import "RoomSwitchTableViewCell.h"
#import "ProjectModel.h"
@interface RoomSwitchTableViewCell()
@property (nonatomic,strong) UILabel *roomLabel;
@end
@implementation RoomSwitchTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
_roomImageV = [[UIImageView alloc]init];
_roomImageV.userInteractionEnabled = YES;
[self addSubview:_roomImageV];
_roomLabel = [[UILabel alloc]init];
_roomLabel.font = [UIFont systemFontOfSize:13*applicationScale];
_roomLabel.textAlignment = NSTextAlignmentLeft;
_roomLabel.textColor = [UIColor grayColor];
[self addSubview:_roomLabel];
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
_roomImageV.frame = CGRectMake(10*applicationScale, 5*applicationScale, 15*applicationScale,15*applicationScale);
_roomLabel.frame = CGRectMake(30*applicationScale,25*applicationScale, 200*applicationScale, 15*applicationScale);
}
-(void)setRoomModel:(ProjectModel *)roomModel{
_roomModel = roomModel;
_roomLabel.text = _roomModel.roomName;
}
viewController文件中tableview的代理方法中的处理
其中_beforeSelectRow是用来记录选中的行
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"roomSwitchCell";
RoomSwitchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[RoomSwitchTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.roomModel = self.roomArr[indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// 当来回滑动的时候,因为cell的复用性,需要重新判断下选中行
if (_beforeSelectRow == indexPath.row) {
cell.imageView.image = [UIImage imageNamed:@"yuanquan_select"];
}else{
cell.imageView.image = [UIImage imageNamed:@"yuanquan"];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
_beforeSelectRow = indexPath.row;//记录当前选中行
[tableView reloadData];
}