[英]Insert new row below a tapped row in UITableView
我一直在嘗試在用戶點擊上方的行時在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];
}
我認為您必須在當前索引路徑值之后在數組中插入新值
[contents insertObject:myObject atIndex:10];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.