2011-11-10 70 views
-1

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

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

NSString *CellIdentifier = @"viewAllProductsGridCell"; 

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

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

    cell = gridCell;  

} 

代码U将请告诉我如何创建多个实例为每个单元

+0

后置代号 - (UITableViewCell的*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath' – Nekto

回答

0

它看起来像你使用你所有的行相同的细胞实例。
您必须为每一行创建一个单独的单元实例。

更新:

我通常是多个定制单元做的是以下几点:

  • 添加一个空的笔尖文件到项目
  • 设置文件的所有者来的UIViewController
  • 将'将UITableViewCell拖放到笔尖空间
  • 将文件的所有者视图属性与UITableViewCell实例
  • 不要忘记通过改变其性质 或将控件添加到它
  • 如果你在的UITableViewCell属性ReuseID设置为 预设值(如textFieldCell)
  • 自定义您的细胞就像你需要需要对自定义单元格进行复杂的控制,在项目中创建一个来自UITableViewCell的 子类,并添加所需的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; 
} 
相关问题