有自定义文本字段单元格,根据用户输入添加到表格 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 property
和 self.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/