当用户点击上方的行时,我一直在尝试在UITableView中动态添加新行。此处似乎还有2-3个其他帖子,但不知何故无法连接/利用相同的帖子。
以下是我的代码。有人可以给我一个想法,请问我可能会出错吗?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
NSInteger rowsRequired = 0;
if (section == 0) {
rowsRequired = 1;
}
else {
rowsRequired = [[self contents] count] + 1;
}
return rowsRequired;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
CellType1 *cell1 = nil;
CellType2 *cell2 = nil;
if (indexPath.section == 0) {
cell1 = [tableView dequeueReusableCellWithIdentifier:@“CellType1”];
if (cell1 == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@“CellType1” owner:self options:nil];
cell1 = [nib objectAtIndex:0];
}
return cell1;
}
else {
cell2 = [tableView dequeueReusableCellWithIdentifier:@“CellType2”];
if (cell2 == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@“CellType2” owner:self options:nil];
cell2 = [nib objectAtIndex:0];
}
return cell2;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1) {
NSIndexPath *newPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
indexPath = newPath;
[[self contents] insertNewRow:@“My New Row”];
[[self tableView] insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
答案 0 :(得分:2)
我认为你必须在当前索引路径值
之后在数组中插入新值 [contents insertObject:myObject atIndex:10];