【问题标题】:Light about a solution to draw cell rectangles关于绘制单元格矩形的解决方案
【发布时间】:2011-11-10 16:41:06
【问题描述】:

How to customize the background/border colors of a grouped table view cell?

阅读这篇文章后,我尝试使用此解决方案。这是我的代码,在 tableViewDelegate 方法中:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
   CustomCellBackgroundView *customBackground = [CustomCellBackgroundView alloc];

    [customBackground setBorderColor:[UIColor blackColor]];
    [customBackground setFillColor:[UIColor redColor]];
    [customBackground setPosition:0]; //i'll deal with that later

    [cell setSelectedBackgroundView:customBackground];
    [customBackground release];


    UIImageView* selectedBackgroundCell = [[[UIImageView alloc] initWithFrame:CGRectNull] autorelease];
    [selectedBackgroundCell setImage:[UIImage imageNamed:@"cell_bg_50_hover.png"]];
    [customBackground drawRect:selectedBackgroundCell.frame];

    [cell setSelectedBackgroundView:selectedBackgroundCell];

    //standard background color
    [cell setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_bg_50.png"]]];
}

但不幸的是,它并没有改变任何东西。你知道我做错了什么吗?

【问题讨论】:

    标签: iphone ios


    【解决方案1】:

    我相信,无论何时制作视图,您都应该为此指定一些框架。 因此,为自定义背景和背景指定框架 CGRectZero....

    当我测试时,以下对我有用。

        UIView *bkview = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    
        bkview.backgroundColor = [UIColor redColor];
    
        cell.selectedBackgroundView = bkview;
    

    【讨论】:

    • 感谢您的写作......我用代码改变了我的答案。请检查天气是否有帮助....
    • 别忘了发autorelease到bkview!
    • 是的,这行得通。但这不是问题的目的。如果将单元格分组(尝试一下),那就不好了。这就是我使用前一篇文章的原因(见我的第一行)
    【解决方案2】:

    释放 customBackground 然后调用 [customBackground drawRect:selectedBackgroundCell.frame];

    显然,在 nil 指针上调用函数不会做任何事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多