ios - 自定义 uitableviewcell 与文本字段消失

标签 ios objective-c uitableview

有自定义文本字段单元格,根据用户输入添加到表格 View 中。当用户添加多个手机号码时,类似于通讯录。 Nib 使用以下代码注册到表格 View 。

[self.tableView registerNib:[UINib nibWithNibName:@"TextFieldTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:textFieldTableViewCellIdentifier];

添加动态单元格的方法:

-(void)addAccoladesValue
{
    NSMutableArray *DataArray =  [self.tableData[SectionTagAccolades-1][sectionDataKey] mutableCopy];
    NSMutableArray *DataValueArray =  [self.tableData[SectionTagAccolades-1][sectionDataValue] mutableCopy];
    self.accoladesArray = self.accoladesArray ? self.accoladesArray : [NSMutableArray new];
    TextFieldTableViewCell *accoladesCell = [self setupTextField:self.accoladesArray tag:TextFieldTagFirstName placeholder:@"Description" cellTextFieldIdentifier:textFieldTableViewCellIdentifier];
    accoladesCell.hidden = NO;
    [DataArray addObject:accoladesCell];
    [DataValueArray addObject:@""];
    self.tableData[SectionTagAccolades-1][sectionDataKey] = DataArray;
    self.tableData[SectionTagAccolades-1][sectionDataValue] = DataValueArray;
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:SectionTagAccolades-1]] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:SectionTagAccolades-1]] withRowAnimation:UITableViewRowAnimationTop];
    [self.tableView endUpdates];
}

代码似乎按预期工作,但有时单元格会消失,并且有几次新添加的单元格被添加到顶部,这是错误的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TextFieldTableViewCell *cell = (TextFieldTableViewCell*)self.tableData[indexPath.section][sectionDataKey][indexPath.row];
    if ([cell isKindOfClass:[TextFieldTableViewCell class]])
    {
        cell.textfield.tag = indexPath.row;
        cell.textfield.superview.tag = indexPath.section;
        cell.textfield.text = self.tableData[indexPath.section][sectionDataValue][indexPath.row];

    }
    cell.hidden = NO;
    [cell setNeedsUpdateConstraints];
    [cell setNeedsLayout];
    return cell;
}

这种奇怪行为的可能原因是什么?

最佳答案

我实际上可以在这里发现很多可能出错的地方。几件事。

1。不要存储 TableViewCells

您应该手动存储单元格UITableView 会为您处理这件事。 UITableView 将重用滚出 View 的cells 来显示新数据,因此可以与不同的对象 相关联。

tableView:cellForRowAtIndexPath: 中调用 dequeueReusableCellWithIdentifier:forIndexPath: 在你的 tableView 上获取一个单元格,然后用它的数据填充它应该显示。

2。数据结构

看起来您的 self.accoladesArray 包含应该显示在 TextFieldTableViewCells 中的数据。为什么要保留对此的引用,一个 instance propertyself.tableData 中的一个?

如果您只保留实例属性,您的 UITableViewDataSource 方法将归结为

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   // accolades section
   return self.accoladesArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // accolades section
   TextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: textFieldTableViewCellIdentifier forIndexPath: indexPath];
   NSString * text = self.accoladesArray[indexPath.row];
   // custom setup
   return cell;
}

- (void) addAccoladesValue
{
    if (!self.accoladesArray) self.accoladesArray = [NSMutableArray new]; 
    [self.tableView beginUpdates];
    {
        [self.accoladesArray insertObject: @"" atIndex: 0];
        [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:SectionTagAccolades-1]] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    [self.tableView endUpdates];        
}

关于ios - 自定义 uitableviewcell 与文本字段消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39554313/

相关文章:

ios - 导航栏缺失

ios - 在 iOS 7、SDK 7 中关闭 UIActionSheet 后,键盘会立即隐藏并再次显示

objective-c - 找不到 'SenTestCase' 的接口(interface)声明

iphone - 适用于 iOS 4.3 的 xcode5

ios - 为什么 systemLayoutSizeFittingSize 为 UITableViewCell 返回 (0, 0)?

ios - 设置 SpriteKit 节点的位置(游戏中)

ios - 如果启用了模块,tgmath.h 将不起作用

ios - 异步调整 UITableViewCell 与生成的 UIView-Encapsulated-Layout-Height

ios - 章节标题 Tableview iOS 上的操作

ios - Xcode Storyboard 上的并排约束