繁体   English   中英

在UITableView中的点击行下方插入新行

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM