2

我正在使用带有背景的文本标签作为我的cell.accessoryView. 当Table被选中时,我的附件视图背景 ( label.backgroundColor) 消失。我可以通过设置来恢复标签的文本颜色label.highlightedTextColor。但我无法恢复它的背景颜色。有类似的东西highlightedTextBackgroundcolorselectedBackgroundView

UIImage *indicatorImage = [UIImage imageNamed:DISTANCE_BUBBLE];
cell.accessoryView = [[UIImageView alloc]initWithImage:indicatorImage];


distanceLabel=[[UILabel alloc]initWithFrame:cell.accessoryView.bounds];

distanceLabel.textColor=[UIColor whiteColor];
distanceLabel.textAlignment=UITextAlignmentCenter;
distanceLabel.font=[UIFont boldSystemFontOfSize:13.0];
distanceLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:DISTANCE_BUBBLE]];
distanceLabel.numberOfLines = 0;
distanceLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.accessoryView=distanceLabel;
4

3 回答 3

1

子类 UITableViewCell。

它会给你更多的控制,让你的 cellForRowAtIndexPath 方法更整洁。

于 2013-01-07T09:36:19.530 回答
1

将您的附件视图创建为一个视图,其中包含一个视图中的背景和标签。

于 2013-01-07T09:12:15.893 回答
0

编写以下代码可能对您有所帮助。

///////////////////////////////////////// ///////////// 编辑代码//////////////////////////// ////////////////////////////////////

用于显示单元格辅助视图的背景颜色

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Foobar"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Foobar"];

        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.textLabel.textColor = [UIColor blackColor]; 
    }
    UIImageView *colorView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

    CGRect rect = CGRectMake(0.0f, 0.0f, 30.0f, 30.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    [colorView setImage:image];

    [cell setAccessoryView:colorView];

    cell.textLabel.text=[self.listOfFrnd objectAtIndex:indexPath.row];

    return cell;
}

显示单元格的背景图 附件视图

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Foobar"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Foobar"];

        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.textLabel.textColor = [UIColor blackColor];
    }
    UIImageView *colorView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];

    CGRect rect = CGRectMake(0.0f, 0.0f, 30.0f, 30.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
    CGContextFillRect(context, rect);

    UIGraphicsEndImageContext();

    [colorView setImage:[UIImage imageNamed:@"apple.png"]];

    [cell setAccessoryView:colorView];


    cell.textLabel.text=[self.listOfFrnd objectAtIndex:indexPath.row];

    return cell;
}

在我尝试之后,发现如果您使用超过一个单元的accessoryView则它只显示一个accessoryView。我不确定我是对的,但可能是真的:) 谢谢 :)

于 2013-01-07T06:34:41.563 回答