本文介绍了UITableViewController以编程方式访问静态单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含10个静态单元格的表格,有没有办法以编程方式选择某个单元格?

Say I have a table with 10 static cells in it, is there a way to select a certain cell programmatically?

我试过这个

UITableViewCell *cell = [self.tableView.subviews objectAtIndex:indexPath.row];

但实际上并没有返回表格单元格。

but that does not actually return a table cell it seems.

这似乎导致我的代码崩溃

this seems to crash my code

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

我试图在代码中设置静态单元格的各个高度。一个选项是为每个静态单元格设置出口,但这看起来很傻。

Im trying to set the individual heights for the static cells in code. An option would be to make outlets for each individual static cell, but that seems silly.

推荐答案

要访问静态创建的单元格,请尝试这个:

To access statically created cells, try this:

UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

这适用于静态单元格。所以,如果你在... ...

This works for static cells. So, if you're in the...

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

     UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    return cell;

}

...委托,您可以访问所有静态配置的单元格使用上述声明。从那里,你可以用单元格做任何你想做的事。

... delegate, you can access all statically configured cells using the above declaration. From there, you can do what ever you want with "cell".

我有一个ViewController上有两个UITableViews。其中一个具有静态定义的单元,具有Storyboard,另一个具有使用代码动态定义的单元。鉴于我使用相同的ViewController作为两个表的委托,我需要阻止创建新单元格,其中调用cellForRowAtIndexPath,其中已经创建了单元格。

I had a ViewController that had two UITableViews on it. One of them had cells defined statically, with a Storyboard, and the other had cells defined dynamically using code. Given I was using the same ViewController as delegate for both tables, I needed to prevent new cells from being created where cellForRowAtIndexPath was being called where cells had already been created.

In在你的情况下,你需要以编程方式访问你的单元格。

In your case, you need to gain programmatic access to your cells.

玩得开心。

这篇关于UITableViewController以编程方式访问静态单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 05:26