使用UITextField在tableView中显示100个自定义单元格

时间:2011-11-10 11:22:28

标签: objective-c ios ipad uitableview

我有一个自定义单元格,我在表格视图中显示该自定义单元格,方法numberOfRowsInSection返回100

但是当我编辑其中一个textfield时,它也会反映其他文本字段中的更改。 这是我的cellForROwAtIndexPath代码

NSString *CellIdentifier = @"viewAllProductsGridCell";

ViewAllProductsGridCell *cell = (ViewAllProductsGridCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"ViewAllProductsGridCell" owner:self options:nil];

    cell = gridCell;    

}

请问我们如何为每个单元格创建多个实例

1 个答案:

答案 0 :(得分:0)

看起来您正在为所有行使用相同的单元格实例 您必须为每一行创建一个单独的单元格实例。

<强>更新

我通常对多个自定义单元格执行的操作如下:

  • 将空的nib文件添加到项目
  • 将文件所有者设置为UIViewController
  • 将UITableViewCell拖放到笔尖空间
  • 使用UITableViewCell实例连接文件的所有者视图属性
  • 不要忘记将UITableViewCell属性中的ReuseID设置为 预定义值(如textFieldCell)
  • 通过更改其属性,根据需要自定义您的单元格 或添加控件
  • 如果您需要对自定义单元格进行复杂控制,请创建一个 来自项目中UITableViewCell的子类,并添加声明all 您需要的IBOutlets或IBActions;别忘了改变 您的单元格的类属性,在MyTextFieldCell的笔尖

完成自定义后,您可以使用以下代码:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"textFieldCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil) {
        UIViewController *theController = [[UIViewController alloc] initWithNibName:@"TextFieldCell" bundle:nil];
        cell = (MyTextFieldCell *)[[theController.view retain] autorelease];
        [theController release];

        NSLog(@"Cell created %@", cell);
    }

    return cell;
}