根据部分自定义 TableviewCell

发布于 2024-08-23 13:27:07 字数 121 浏览 5 评论 0原文

如何根据 UITableViewCell 所在的部分自定义它们的背景图像?例如,在第一部分中,单元格应该具有绿色 .png 图像作为背景图像,而在第二部分中,单元格应该具有红色 .png 图像作为背景。

How do I customize the background image of a UITableViewCell depending on the section, they are in? E.g. in section one, the cells are supposed to have a green .png image as a background image, whereas in section two, the cells are supposed to have a red .png image as background.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独夜无伴 2024-08-30 13:27:07

请参阅用数据填充表视图了解如何访问和使用节信息。

当您有多个节时,您可以实现一些可选的表视图源方法来处理节。

然后,当您创建单元格时,检查它所在的部分并进行相应的设置,在:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath;

在此方法中,您可以访问 indexPath.section 并格式化单元格。

请参阅 表视图编程指南了解更多

UIKit 添加属性 部分NSIndexPath ,这样您就可以从传递到上述方法的索引路径获取 section (请参阅我链接的第一个示例)。

See Populating the Table View With Data for how to access and use section information.

When you have more then one section you implement some optional table view source methods to deal with sections.

Then when you create cell, check in which section it is and set it up accordingly, in:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath;

In this method you can access indexPath.section and format your cell.

See Table View Programming Guide for more.

UIKit adds property section to NSIndexPath so you can get section from index path passed in to the above method (see the first example I linked).

灰色世界里的红玫瑰 2024-08-30 13:27:07

例子:

if(section == 0) {
background.image = [UIImage imageNamed:@"green.png"];


}
else if(section == 1) {

background.image = [UIImage imageNamed:@"blue.png"];

}

example:

if(section == 0) {
background.image = [UIImage imageNamed:@"green.png"];


}
else if(section == 1) {

background.image = [UIImage imageNamed:@"blue.png"];

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文