ITableViewController以编程方式访问静态单元格

ITableViewController以编程方式访问静态单元格

本文介绍了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.

这似乎使我的代码崩溃

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.其中一个具有使用情节提要静态定义的单元格,另一个具有使用代码动态定义的单元格.鉴于我在两个表中都使用与委托相同的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 your case, you need to gain programmatic access to your cells.

玩得开心.

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

08-26 05:26