我对如何自定义UITableViewCell
高亮样式,不仅高亮显示的单元格背景颜色以及其子视图(图像视图)的框架感到困惑?
的
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
方法对我不起作用。如果仅更改单元格的突出显示颜色,则效果很好。但这不适用于更改单元格子视图的框架。
谢谢你的帮助!
最佳答案
您可以按照以下方式进行
下面是它的示例代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *strIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageView *imgv = [[UIImageView alloc] initWithFrame:CGRectMake(278, 2, 40, 40)];
[imgv setBackgroundColor:[UIColor grayColor]];
imgv.tag = 100 + indexPath.row;
[cell.contentView addSubview:imgv];
[cell.textLabel setText:[NSString stringWithFormat:@"Cell: %d",indexPath.row]];
[cell.detailTextLabel setText:[NSString stringWithFormat:@"%03d - %03d",indexPath.section, indexPath.row]];
return cell;
}
-(void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.textLabel setBackgroundColor:[UIColor orangeColor]];
//set the frame of subview on highlight
UIImageView *imgv = (UIImageView*)[cell.contentView viewWithTag:100+indexPath.row];
[imgv setBackgroundColor:[UIColor orangeColor]];
[imgv setFrame:CGRectMake(238, 2, 80, 40)];
}
-(void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
//now reset the frame of subview on Unhighlight
UIImageView *imgv = (UIImageView*)[cell.contentView viewWithTag:100+indexPath.row];
[imgv setBackgroundColor:[UIColor grayColor]];
[imgv setFrame:CGRectMake(278, 2, 40, 40)];
}