1

我正在做一个项目,其中我有一个 TableView,其中我有不同的行/单元格,并且在每个单元格中都有一个添加按钮。我需要做的是单击添加按钮,新行应添加到我单击按钮的行下方,这意味着我必须在该行上选定按钮的下一个添加一个单元格。用户也可以输入新行上的文本。请告诉我该怎么做。因为我是 iPhone 开发的新手。我将非常感谢....

这是一个单击按钮功能,我想在其中执行添加新单元格的操作。

- (IBAction)AddButtonAction:(id)sender
{
    [_choices insertObject:@"avav" atIndex:[_choices count]];
    [self.tableView reloadData];
    NSLog(@"here");

}
4

3 回答 3

2

使用此委托方法在所选行下方添加新行。并使用自定义单元格在其上有一个文本字段......

     rowCount ++;//Here rowcount refers the no. of rows in the table. 
     selectedIndexPath = indexPath;//Assign the selected indexpath for creating custom cell on it. 
    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];//Indexpath refers the currently selected/targeted cell. 

在 cellforRowAtIndexPath 中像这样使用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if((selectedIndexPath) && (indexPath.row == selectedIndexPath.row) && (indexPath.section == selectedIndexPath.section))
    {
        static NSString *cellIdentifier=@"cell";
        NewCell *cell = (NewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil)
        {
            NSString *customeCellName = [NSString stringWithFormat:@"%@",[NewCell class]];

            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:customeCellName owner:self options:nil];

            for (id currentObject in topLevelObjects)
            {
                if ([currentObject isKindOfClass:[UITableViewCell class]])
                {
                    cell =  (NewCell *) currentObject;
                    break;
                }
            }
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];


        return cell;
    }
    else
    {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];

    // Configure the cell...

    return cell;
    }
}

输出将是这样的 在此处输入图像描述

需要根据您的要求进行定制。

于 2013-03-13T13:05:14.633 回答
0

如果通过addSubview方法将按钮添加到单元格,只需使用以下代码即可获得 indexPath:

- (IBAction)AddButtonAction:(id)sender
{
    UIButton *button = (UIButton *)sender;
    UITableViewCell *cell = (UITableViewCell *)[button superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    // Adding methods comes here...
}

如果您使用某些addSubview级别,您应该使用相同superview的方法反向访问 UITableViewCell 级别。如果使用自定义单元格,请将上述代码中的自定义单元格类名替换为 UITableViewCell。

要添加单元格,请查看WWDC 2011 视频,“UITableView Changes, Tips & Tricks”视频。它展示了一些不错且有用的方法来在其他行/节之间插入、删除和重新加载新行/节,例如insertRowsAtIndexPaths:withRowAnimation:方法。或查看Apple 文档

于 2013-04-10T12:20:26.287 回答
0

我认为您在所有单元格中都有 textfeild 。在 AddButtonAction 函数中保存行的索引。在创建单元格时,使选定的单元格文本成为第一响应者。用户输入数据后,将其保存在数组中并再次重新加载。只需正确跟踪 selectedIndex 即可。如果觉得这会有所帮助。

于 2013-03-13T13:07:26.070 回答