您好我正在尝试为分组表视图部分的背景设置图像(不适用于部分标题)我正在使用以下代码: -
-(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
超过某个单元格。
我想像下面的屏幕截图一样设置图片: -
答案 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];
}