xcode 带有没有 cellForRowAtIndexPath 的静态单元格的 UITableView。如何设置清晰的背景?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11993798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:15:16  来源:igfitidea点击:

UITableView with static cells without cellForRowAtIndexPath. How to set clear background?

iosxcodecocoa-touchuitableview

提问by Darren

I have a static UITableView and everything is laid out on the Storyboard. In cellForRowAtIndexPath i simply use

我有一个静态的 UITableView,一切都在 Storyboard 上。在 cellForRowAtIndexPath 我只是使用

return [super tableView:tableView cellForRowAtIndexPath:indexPath];

and the tableview works great. However I want to set the cells backgrounds to [UIColor clearColor] How can I do this? The table has 30 cells, each one different.

并且 tableview 很好用。但是我想将单元格背景设置为 [UIColor clearColor] 我该怎么做?该表有 30 个单元格,每个单元格都不同。

Thanks

谢谢

回答by Jonathan Ellis

I'm not entirely sure if this will work, because I've never tried using superto manage the cells. If your code works with super, then you should be able to get away with doing this to set the background color:

我不完全确定这是否有效,因为我从未尝试过使用它super来管理单元格。如果您的代码适用于super,那么您应该能够通过这样做来设置背景颜色:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

Either way, this seems to me like a strange way of doing things. Normally the cells are created and reused in the method itself, as per the default UITableViewboiletplate code, without calling super.

无论哪种方式,在我看来,这都是一种奇怪的做事方式。通常,根据默认的UITableView样板代码,在方法本身中创建和重用单元格,而不调用super.

回答by CSmith

Try this delegate method:

试试这个委托方法:

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
 cell.backgroundColor = [UIColor clearColor];
}

回答by 8vius

You can set up a method and call it in your viewDidLoad in order to get all the cells and do whatever you want with them you can use this:

您可以设置一个方法并在您的 viewDidLoad 中调用它以获取所有单元格并对其进行任何您想做的事情,您可以使用它:

NSArray *visibleIndexPaths = [self.tableView indexPathsForVisibleRows];

To get all the index paths of the currently viewable cells

获取当前可见单元格的所有索引路径

Then iterate over them in the following way

然后按以下方式迭代它们

UITableViewCell *cell = nil;
for (int i = 0; i < 30; i++) {
  cell = [self.tableView cellForRowAtIndexPath:[visibleIndexPaths objectAtIndex:i]];
  // Do your setup
}

You can also reference this Apple developer documentthe part "The Technique for Static Row Content"

您还可以参考此 Apple 开发人员文档的“静态行内容技术”部分