UITableViewCell重叠

时间:2013-09-18 08:08:34

标签: iphone ios uitableview overlapping

我正在寻找创建一个包含重叠单元格的uitableview,如下图所示。问题是即使我为单元格的内容视图设置了clipsToBounds = NO,也不会显示单元格假标题(例如西班牙语,它将与前一个单元格重叠)。

有谁知道如何解决这个问题?

enter image description here

这是我的代码:

- (UITableViewCell *)tableviewCellTournamentWithReuseIdentifier:(NSString *)identifier andPosition:(NSInteger)pos {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
if (pos == 0) {
    cell.clipsToBounds = NO;
    cell.contentView.clipsToBounds = NO;
    cell.frame = CGRectMake(0, 0, tournamentsTableView.frame.size.width, CELL_HEIGHT);

    UIView* row = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tournamentsTableView.frame.size.width, CELL_HEIGHT)];
    row.clipsToBounds = NO;
    row.backgroundColor = [UIColor clearColor];

    UIView* topRow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 10)];
    topRow.backgroundColor= [UIColor colorWithRed:210/255.0 green:131/255.0 blue:62/255.0 alpha:1.0];
    [row addSubview:topRow];

    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, row.frame.size.width, CELL_HEIGHT-10)];
    label.backgroundColor = [UIColor colorWithRed:63*pos/255.0 green:71*pos/255.0 blue:113/255.0 alpha:1.0];
    label.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
    label.tag = TOURNAMENT_NAME;
    label.layer.cornerRadius = 5;
    [row addSubview:label];

    [cell.contentView addSubview:row];
} else {
    cell.clipsToBounds = NO;
    cell.contentView.clipsToBounds = NO;
    cell.frame = CGRectMake(0, -10, tournamentsTableView.frame.size.width, CELL_HEIGHT);

    UIView* row = [[UIView alloc] initWithFrame:CGRectMake(0, -10, tournamentsTableView.frame.size.width, CELL_HEIGHT)];
    row.clipsToBounds = NO;
    row.backgroundColor = [UIColor clearColor];

    UIView* topRow = [[UIView alloc] initWithFrame:CGRectMake(0, -10, 100, 10)];
    topRow.backgroundColor= [UIColor colorWithRed:210/255.0 green:131/255.0 blue:62/255.0 alpha:1.0];
    [row addSubview:topRow];
    [cell bringSubviewToFront:tournamentsTableView];

    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, row.frame.size.width, CELL_HEIGHT-10)];
    label.backgroundColor = [UIColor colorWithRed:63*pos/255.0 green:71*pos/255.0 blue:113/255.0 alpha:1.0];
    label.font = [UIFont fontWithName:@"HelveticaNeue" size:14];
    label.tag = TOURNAMENT_NAME;
    label.layer.cornerRadius = 5;
    [row addSubview:label];

    [cell.contentView addSubview:row];
}

return cell;

}

结果是: enter image description here

1 个答案:

答案 0 :(得分:1)

问题是你正试图操纵tableview它的自我,这就是你遇到问题的原因。您将需要在单元格中包含“选项卡”,因为它更容易添加任意数量的出口,因为它自己充当单独视图的单元格。

将您的第一行添加到单元格

UIView *topRow = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 10.0)];
topRow.backgroundColor= [UIColor colorWithRed:210/255.0 green:131/255.0 blue:62/255.0 alpha:1.0];
[cell.contentView addSubview:topRow];

然后将您的主要内容添加到单元格并将其向下移动

UIView* row = [[UIView alloc] initWithFrame:CGRectMake(0.0, 10.0, tournamentsTableView.frame.size.width, CELL_HEIGHT)];
row.clipsToBounds = NO;
row.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:row];

最终摆脱了细胞的背景颜色,你几乎有了你的效果。

cell.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor clearColor];

希望有所帮助:)