2012-08-16 20 views

回答

56

我不能完全肯定这是否会工作,因为我从来没有使用super管理单元尝试。如果你的代码工作与super,那么你应该能够逃脱这样做是为了设置背景色:

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

无论哪种方式,这似乎对我来说,做事情的一种奇怪的方式。通常情况下,单元格按照缺省UITableView boiletplate代码在方法本身中创建并重新使用,而不呼叫super

+2

谢谢,这很好。由于表格非常长,每个单元格都是完全不同和静态的,因此我发现将它们全部置于Storyboard中并将IBOutlet创建为所有需要数据的UILabel更容易。 – Darren 2012-08-16 19:54:04

+0

@Jonathan谢谢 – 2014-04-16 03:26:36

+0

谢谢!你拯救了一天! – 2015-10-28 08:41:51

1

您可以设置的方法,为了把它在viewDidLoad中得到所有的细胞,做任何你想要他们,你可以这样做:

NSArray *visibleIndexPaths = [self.tableView indexPathsForVisibleRows]; 

要获得所有当前能够观察的细胞

的指数路径,然后用迭代它们以下述方式

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

您也可以参考this Apple developer document部分“静态行内容技术”

+0

这种方法没有工作,但感谢的答案。 – Darren 2012-08-16 19:51:32

+0

没问题,现在我可以重构我的代码更好 – 8vius 2012-08-16 19:58:30

17

试试这个委托方法:

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

这种方法确实奏效,谢谢。然而,我选择jon的答案是正确的,因为它似乎是设置单元格的正确位置。谢谢 – Darren 2012-08-16 19:52:35

+1

接受的答案有效,但不必要的冗长和复杂。 'willDisplayCell:'更简单明了,非常适合用例。 – 2014-09-29 02:06:29

相关问题