自定义tableviewCell的选中效果

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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值