iOS 点击cell改变背景颜色或者图片&&cell中其他部分取消高亮显示

点击Cell改变背景图片或颜色时不能添加selectionStyle,如果添加更改也没有效果

以下这句话不能添加:

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

点击更改背景图片:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIImageView * imageView = [[[UIImageView alloc] initWithFrame:cell.frame] autorelease];
    imageView.image = [UIImage imageNamed:@"cell_backgroud"];
    cell.backgroundView = imageView;
    
    UIImageView * imageViewSelect = [[[UIImageView alloc] initWithFrame:cell.frame] autorelease];
    imageViewSelect.image = [UIImage imageNamed:@"cell_backgroud_select"];
    cell.selectedBackgroundView = imageViewSelect;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

这样就可以了(我选的背景图片替换不是很明显,但是我的第一条cell是点击了,已经可以改变了)。


改变背景颜色也是一样

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellTableIdentifier = @"CellTableIdentifier";
    
    //该方法第一次调用时返回nil(因为程序刚开始运行时并没有可以重用的UITableViewCell)
    MyCell * cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:cellTableIdentifier];
    if (cell == nil) {
        //加载NearCell.xib
        NSArray * array = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
    }
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIView * view1 = [[UIView alloc] initWithFrame:cell.frame];
    view1.backgroundColor = [UIColor redColor];
    cell.backgroundView = view1;
    
    UIView * view2 = [[UIView alloc] initWithFrame:cell.frame];
    view2.backgroundColor = [UIColor cyanColor];
    cell.selectedBackgroundView = view2;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}




那么这时候就有一个问题,cell当中的Button或者Label也会高亮显示 ,上面的cell中黑色字体就会变白色,那么怎么解决这个问题呢?在你自定义的Cell中修改

#import "NearCell.h"

@implementation NearCell

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

    // Configure the view for the selected state
    
    if (selected) {
        [self.merchantTitle setHighlighted:NO];
        [self.merchantDescription setHighlighted:NO];
        [self.merchantAddress setHighlighted:NO];
        [self.distance setHighlighted:NO];
    }
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    if (highlighted) {
        [self.merchantTitle setHighlighted:NO];
        [self.merchantDescription setHighlighted:NO];
        [self.merchantAddress setHighlighted:NO];
        [self.distance setHighlighted:NO];        
    }
}


上面就是将你cell中定义的属性setHighlighted:NO,这样你点击cell时候只是会改变背景,不会将cell中的其他部分高亮显示。这样就OK啦,希望对你有帮助。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值