如何为分组表部分设置不同的背景图像..?

时间:2013-12-26 13:25:35

标签: ios uitableview ios7

您好我正在尝试为分组表视图部分的背景设置图像(不适用于部分标题)我正在使用以下代码: -

-(void)addBackgroundViewForSectionIndex:(int)tagIndex {

    for (UIView * subview in tbl_home.subviews) {
        if(subview.tag == tagIndex)
            [subview removeFromSuperview];
    }

    CGRect sectionFrame = [tbl_home rectForSection:tagIndex];
    UIImageView *newView = [[UIImageView alloc]initWithFrame:sectionFrame];
    newView.tag = tagIndex;
    [newView setImage:[UIImage imageNamed:@"Section@2x.png"]];
    [tbl_home addSubview:newView];
    [tbl_home sendSubviewToBack:newView];

}

将图像设置为背景但有问题是某些单元格未显示或newView超过某个单元格。

我想像下面的屏幕截图一样设置图片: -

enter image description here

1 个答案:

答案 0 :(得分:3)

经过一些更改后,现在帮助我使用此代码成功地将图像设置为上图: -

-(void)addBackgroundViewForSectionIndex:(int)tagIndex {

    for (UIView * subview in tbl_home.subviews) {
        if(subview.tag == tagIndex)
            [subview removeFromSuperview];
    }

    CGRect sectionFrame = [tbl_home rectForSection:tagIndex];
    UIImageView *newView = [[UIImageView alloc]initWithFrame:sectionFrame];
    newView.tag = tagIndex;
    [newView setImage:[UIImage imageNamed:@"Section@2x.png"]];
    [tbl_home addSubview:newView];
    [tbl_home sendSubviewToBack:newView];

}

previouselly我将此方法调用到cellForRowIndex,以便每次都调用并创建问题。现在我在

中调用此方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

就像轰鸣声,现在工作正常: -

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
  [self addBackgroundViewForSectionIndex:indexPath.section];
}