gpt4 book ai didi

iphone - 在 iOS 中将单元格添加到 UITableView 的底部

转载 作者:可可西里 更新时间:2023-11-01 03:04:44 26 4
gpt4 key购买 nike

我正在使用带有 Storyboard的 xcode 4.2 来创建一个 iphone 应用程序。

当我按下右上角的编辑按钮时,我希望可以选择删除现有行并在顶部看到额外的单元格(带有绿色“+”图标),这样我就可以添加一个新细胞。

我有一个数组,它使用 CoreData 在 viewDidLoad 方法中填充

我已经启用了设置按钮

self.navigationItem.rightBarButtonItem = self.editButtonItem;

并实现了方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:   
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:
(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// removing a cell from my array and db here...
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// adding a cell to my array and db here...
}
}

我意识到我需要在某个点添加单元格,然后我可以对其进行编辑,但我不清楚在哪里,而且我无法在 Internet 上找到解释。

最佳答案

基本思路是,当单击编辑按钮时,我们将在每一行旁边显示删除控件,并使用添加控件添加新行,以便用户可以单击它来添加条目,对吗?首先,由于您已经设置了编辑按钮,让我们指示我们的表格在编辑模式下我们应该显示一个额外的行。我们在 tableView:numberOfRowsInSection 中这样做:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.editing ? a_recs.count + 1 : a_recs.count;
}

a_recs 这是我设置的用于存储记录的数组,因此您必须将其切换为您自己的数组。接下来我们告诉 tableView:cellForRowAtIndexPath: 如何处理额外的行:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"Cell";
BOOL b_addCell = (indexPath.row == a_recs.count);
if (b_addCell) // set identifier for add row
CellIdentifier = @"AddCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (!b_addCell) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}

if (b_addCell)
cell.textLabel.text = @"Add ...";
else
cell.textLabel.text = [a_recs objectAtIndex:indexPath.row];

return cell;
}

我们还想指示我们的表,对于该添加行,我们需要添加图标:

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == a_recs.count)
return UITableViewCellEditingStyleInsert;
else
return UITableViewCellEditingStyleDelete;
}

黄油。现在用筷子把它夹在一起的 super secret 功夫酱:

-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
if(editing) {
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:a_recs.count inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
} else {
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:a_recs.count inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
// place here anything else to do when the done button is clicked

}
}

祝你好运,胃口好!

关于iphone - 在 iOS 中将单元格添加到 UITableView 的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9874917/

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