2

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

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

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

在我的 TextField 类的@implementation 中,在 - (id)initWithStyle: 中,我试图通过以下方式访问 indexPath:

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

...为了设置文本字段的标签,如:

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

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

谢谢

4

1 回答 1

3

我认为问题可能是在初始化您的单元格时它尚未添加到 UITableView - 因此 self.superview 返回零。

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 将被传递给该方法。

设置标记方法应如下所示:

-(void)setTag:(int)tag {
    textRow.tag = tag;
}
于 2011-02-19T13:32:28.370 回答