【发布时间】:2016-09-18 21:15:52
【问题描述】:
有根据用户输入添加到 tableview 的自定义文本字段单元格。当用户添加多个手机号码时,类似于通讯录。 Nib 使用以下代码注册到表格视图。
[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;
}
这种奇怪行为的可能原因是什么?
【问题讨论】:
标签: ios objective-c uitableview