将单元格添加到iOS中的UITableView底部
问题描述:
我正在使用xcode 4.2与故事板创建iphone应用程序。将单元格添加到iOS中的UITableView底部
当我按下右上角的编辑按钮,我想要删除现有的行,并看到额外的单元格(与绿色的'+'图标)在顶部,这将允许我添加一个新的细胞。
我有正被填充在使用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...
}
}
原来自己需要的阵列在某些时候添加单元格,然后我可以编辑它,但不清楚在哪里以及我无法在互联网上找到解释。
答
好的,基本的想法是,当单击编辑按钮时,我们将显示每行旁边的删除控件,并添加一个新的行与添加控件,使用户可以点击它,以添加一个条目右?首先,由于您已经设置了编辑按钮,所以让我们指导我们的表格,在编辑模式下,我们应该显示额外的一行。我们这样做,在我们的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;
}
黄油。现在超级秘密功夫酱,用筷子把它们全部放在一起:
-(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
}
}
祝你好运和好胃口!
答
This tutorial是值得一读,应该帮助你。它展示了如何建立一个在一个UITableView底部的“添加新行”的UITableView行,但你应该能够使这个出现在您的UITableView顶部的实施中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
例如
if(self.editing && indexPath.row == 1)
{
cell.text = @"Insert new row";
...
希望这有助于!
:))没问题! – ragamufin 2012-03-26 20:08:57
使用此方法时,我注意到我们添加和删除的插入行在删除它时会在右侧显示蓝色选择图标。它必须在删除行之前将表从编辑模式中取出。我试着将'beginUpdates'调用放在tableView'setEditing'调用的上方,但是当添加行时出现蓝色图标,而不是删除。任何想法如何避免这个蓝色选择图标? – Ryan 2013-01-08 02:05:18
听起来像accessoryType被设置在某个地方或缓存在tableView队列中。尝试在添加单元格时将其明确设置为UITableViewCellAccessoryNone。 – ragamufin 2013-01-08 18:16:52