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

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];

}

将图片设置为背景,但有些单元格无法显示或者newView覆盖了一些单元格。

我想要像下面的截图一样设置图片:

enter image description here


1
Nitin 给我一个样例代码,我会为你完成它。 - Nirav Jain
1个回答

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];

}

之前我把这个方法放在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];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接