假设我有一个包含 10 个静态单元格的表格,有没有办法以编程方式选择某个单元格?
我试过了
UITableViewCell *cell = [self.tableView.subviews objectAtIndex:indexPath.row];
但这似乎并没有真正返回一个表格单元格。
这似乎让我的代码崩溃了
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
我正在尝试为代码中的静态单元格设置单独的高度。一种选择是为每个单独的静态单元制作 socket ,但这看起来很愚蠢。
最佳答案
要访问静态创建的单元格,试试这个:
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
这适用于静态单元格。所以,如果你在...
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
return cell;
}
...委托(delegate),您可以使用上述声明访问所有静态配置的单元格。从那里,您可以用“cell”做任何您想做的事。
我有一个 ViewController,上面有两个 UITableView。其中一个使用 Storyboard 静态定义单元格,另一个使用代码动态定义单元格。鉴于我使用相同的 ViewController 作为两个表的委托(delegate),我需要防止在已创建单元格的地方调用 cellForRowAtIndexPath 时创建新单元格。
在您的情况下,您需要以编程方式访问您的单元格。
玩得开心。
关于ios - UITableViewController 以编程方式访问静态单元格问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14726943/