Cocoa touch 我们可以在UITableview中为部分设置背景图像吗?
我们是否可以为Cocoa touch 我们可以在UITableview中为部分设置背景图像吗?,cocoa-touch,uitableview,Cocoa Touch,Uitableview,我们是否可以为UITableView部分设置背景图像,如UITableViewCellbackGroundView。根据我的研究,这是不可能的&我们需要定制它的类似行的部分。 除此之外,我们还能得到更好的解决方案吗 提前谢谢 您可以设置TableView部分的背景 查看委托方法,如下所示: - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIImage
UITableView
部分设置背景图像,如UITableViewCell
backGroundView
。根据我的研究,这是不可能的&我们需要定制它的类似行的部分。除此之外,我们还能得到更好的解决方案吗
提前谢谢 您可以设置TableView部分的背景 查看委托方法,如下所示:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImageView *bgImgView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0,0.0,tableView.bounds.size.width,40.0)] autorelease];
bgImgView.image = [UIImage imageNamed:@"yourimage.jpg"];
return bgImgView;
}
希望这对你有帮助
干杯 是的,我是这样做的,我想这对你也有用
-(void)addBackgroundViewForSectionIndex:(int)tagIndex {
for (UIView * subview in tableView.subviews) {
if(subview.tag == tagIndex)
[subview removeFromSuperview];
}
CGRect sectionFrame = [tableView rectForSection:tagIndex];
UIImageView *newView = [[UIImageView alloc]initWithFrame:sectionFrame];
newView.tag = tagIndex;
[newView setImage:[UIImage imageNamed:@"image.PNG"]];
[tableView addSubview:newView];
[tableView sendSubviewToBack:newView];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [collection objectAtIndex:indexPath.row];
[self addBackgroundViewForSectionIndex:indexPath.section];
return cell;
}
如果有任何问题,请告诉我,…:)否,这不可能按预期使用
UITableView
。正如其他人所建议的那样,部分标题不起作用,因为它们只有在标题可见时才可见。只要它在向上滚动时消失,背景图像就会消失
你的两个选择是:
- 将
实例直接作为子视图添加到表视图中。这并不有趣,因为您必须手动调整它们的大小和位置,这有点难UIImageView
- 使用
,它支持装饰视图(正是您想要的)UICollectionView
UICollectionView
方法可能更好,但老实说,这两种方法都需要大量工作 如果还有人被问到,实际上有一个非常简单的解决方案。UITableView将使用选择器rectForSection告诉您每个节的帧:
将背景图像添加到每个部分后,看起来会像这样(在视图控制器中的某个位置):
for(int i=0;i<[self numberOfSectionsInTableView:self.tableView];i++){
UIImage*img=[[UIImage imageNamed:@“background”]可调整大小的带CapInsets的图像:UIEdgeInsetsMake(30.0,30.0,40.0,40.0)];//在本例中,图像是可拉伸的
UIView*borderView=[[UIImageView alloc]initWithImage:img];
CGRect section_rect=[self.tableView rectForSection:i];
borderView.frame=剖面图;
[self.tableView addSubview:borderView];
}
请记住,如果在tableview中重新加载数据,您也必须重新执行此操作。Jesper的Swift 5裸体实现
用于0中的sectionIndex..这有帮助吗?我需要的是整个部门,而不是部门的负责人。@Madhu你是这样尝试的吗?将单个图像裁剪为多个图像,并分别添加为节标题背景和每个单元格背景。。我想它会像你需要的那样..@R.A完美!!!它看起来会像我们预期的那样工作。但我猜使用这个工具会出现小问题,当滚动表格时,它会修复节标题,直到该节行完成。然后它看起来很奇怪。我想你明白我的意思了…你肯定。。我早就知道了。但我觉得有东西总比没有好。。所以我给了你这个解决方案。。如果我有更好的主意,我会告诉你的。实际上我有一个疑问。当您滚动tableview时,它应该是什么样子??如果你有一个模式,那么这是一个简单的方式来显示为颜色与PatterImage:method在所有的单元格背景和标题也。如果您有一个图像(不是模式),那么当单元格数量有时增加或减少,或者它可能与您提供的图像不匹配时,可能会产生问题。它适用于节标题视图,但我需要查看整个节。我不明白您的意思。你的意思是,只有一个标题或其他任何内容。请详细说明这一点。所以,我可以更好地帮助你。屏幕截图将不胜感激。@Rushi:y坐标(0.0)表示从标题开始。这意味着它将自己获取标题的y坐标。@Rushi:这是标题图像视图高度。您需要实现“heightForHeader”方法,并需要将其高度设置为“40.0”或根据您的需要进行设置。@NishantB:根据您的需要进行更正。那么为什么选择40.0像素?您可以手动创建表格在表格背景中添加图像请不要发布章节标题视图和背景图像逻辑图。我知道背景图像和章节标题视图。已经发展了这么多年,但不知道该方法曾经存在:)
table= [[UITableView alloc] initWithFrame:CGRectMake(7,0, 307, 124) style:UITableViewStylePlain];
[table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tablebg_pt12.png"]];
imageView.frame=CGRectMake(0, 0, 307, 124);
imageView.image= [UIImage imageNamed:@"tablebg_pt12.png" ];
table.backgroundView = imageView;
[imageView release];
table.delegate = self;
table.dataSource = self;
// table.backgroundColor=[UIColor clearColor];
[viewTable addSubview:table];
for(int i = 0;i < [self numberOfSectionsInTableView:self.tableView]; i++) {
UIImage *img = [[UIImage imageNamed:@"background"] resizableImageWithCapInsets:UIEdgeInsetsMake(30.0, 30.0, 40.0, 40.0) ]; //In this example the image is stretchable
UIView *borderView = [[UIImageView alloc] initWithImage:img];
CGRect section_rect = [self.tableView rectForSection:i];
borderView.frame = section_rect;
[self.tableView addSubview:borderView];
}
for sectionIndex in 0..<tableView.numberOfSections {
let backgroundView = UIView() // replace UIView with anything else you want, whether it was UIImage, nibView, etc...
backgroundView.frame = tableView.rect(forSection: sectionIndex)
tableView.addSubview(backgroundView)
}