gpt4 book ai didi

iphone - TableView 添加单元格

转载 作者:行者123 更新时间:2023-11-29 11:02:07 24 4
gpt4 key购买 nike

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

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

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

}

最佳答案

使用此委托(delegate)方法在所选行下方添加新行。并使用自定义单元格在其上设置文本字段...

     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;
}
}

OutPut 会是这样的 enter image description here

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

关于iphone - TableView 添加单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15385805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com