从自定义UITableViewCell访问indexPath [英] Accessing indexPath from custom UITableViewCell

查看:46
本文介绍了从自定义UITableViewCell访问indexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个UITableView以允许用户输入数据,类似于很多UITableViews设置,并带有一些文本字段和其他复选框的注解.对于应用程序中的这种常见功能,这似乎不是很简单.

I'm trying to create a UITableView to allow the user to enter data, similar to a lot of the settings UITableViews, with some textfields and callouts to other tableviews for checkboxes. For such a common feature in apps, this does not seem to be very straight forward.

我无法从自定义UITableViewCell访问indexPath.row.这是我分配自定义UITableViewCell的地方.

I'm having trouble accessing the indexPath.row from my custom UITableViewCell. Here is where I allocate my custom UITableViewCell.

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[TextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];      
}

在我的TextField类的@implementation中,在-(id)initWithStyle中:我正在尝试使用以下方式访问indexPath:

In my TextField class's @implementation, in - (id)initWithStyle: I'm trying to access the indexPath with:

NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell:self];

...以便将文本字段的标签设置为:

... in order to set the textfield's tag like:

textRow = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 21)];
textRow.tag = [indexPath row];

任何人都可以阐明我的问题,或指出我以编程方式创建基本的设置样式TableView的方向.

Could anyone please shed some light on my problem or point me in the direction of creating basic setting-style TableViews programatically.

谢谢

推荐答案

我认为问题可能是在初始化单元格时尚未将其添加到UITableView中,因此self.superview返回nil.

I think the problem might be that at the time of initialisation of your cell it hasn't been added to the UITableView - hence self.superview is returning nil.

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[TextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];      
}
[cell setTag:indexPath.row];

您将需要在您的TextFieldCellClass中添加一个setTag:方法.我认为代码位于cellForIndexPath之内,所以indexPath将传递给该方法.

You will need to add a setTag: method to your TextFieldCellClass. I presume that code was inside cellForIndexPath or whatever, so indexPath will be passed to that method.

set标签方法应如下所示:

The set tag method should look something like this:

-(void)setTag:(int)tag {
    textRow.tag = tag;
}

这篇关于从自定义UITableViewCell访问indexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆