1

在我的项目中,我有带有静态单元格的 tableViews 以及带有动态单元格的 tableViews。为了定制,我设法在单元格上获得了渐变背景(分组样式)。

它适用于动态 TableViews,因为我根据行的位置(顶部、底部、中间或单个)在 cellForRowAtIndex... 中设置背景视图。

但是,当我尝试在静态 tableview 单元格上实现它时,它不起作用。我试图实现 cellForRowAtindex... 但它崩溃了。

有人有想法吗?

更新:cellForRow 的代码..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    

    UACellBackgroundView *bgw=[[UACellBackgroundView alloc]init];

    if (indexPath.row==0) {

        bgw.position = UACellBackgroundViewPositionTop;
        cell.backgroundView=bgw;

    }else if (indexPath.row==2){

        bgw.position = UACellBackgroundViewPositionBottom;
        cell.backgroundView=bgw;

    }else {
        bgw.position = UACellBackgroundViewPositionMiddle;
        cell.backgroundView=bgw;
    }

  //  cell.backgroundView=bgw;


    return cell;
}

顺便说一句,我从这里得到的背景视图: http: //code.coneybeare.net/how-to-make-custom-drawn-gradient-backgrounds 和这里: http: //pessoal.org/blog/2009 /02/25/customizing-the-background-border-colors-of-uitableview/

如果有人感兴趣

4

2 回答 2

1

如果您有一个带有静态表的 UITableViewController 子类,则不应尝试使单元格出列。
相反,您应该要求super提供牢房。超类将从情节提要中获取单元格,您可以对其进行配置。

像这样的东西:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    UIView *selectedBackgroundView = [[UIView alloc] init];
    cell.selectedBackgroundView = selectedBackgroundView;
    cell.selectedBackgroundView.backgroundColor = [UIColor mb_tableViewSelectionColor];
    return cell;
}

也适用于所有其他属性。

于 2013-09-03T10:14:14.603 回答
1

看起来您不是分配了 UITablViewCell,您需要分配单元格。

例如:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        // alloc the UITableViewCell
        // remeber if you are not using ARC you need to autorelease this cell
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = @"Cell Name";
    cell.detailTextLabel.text = @"Cell Detail";

    return cell;
}

添加此语句:

if (cell == nil) {
    // alloc the UITableViewCell
    // remeber if you are not using ARC you need to autorelease this cell
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
于 2012-05-19T19:56:19.567 回答