点击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];
}
#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啦,希望对你有帮助。