[英]User adding rows/sections to static cells in Table View Controller
我想创建一个类似于iPhone Contacts应用程序的“ New Contact”屏幕的屏幕。 在“添加电话”,“添加电子邮件”等旁边没有绿色的“ +”号。当用户单击这些时,会创建新的行(或者在“添加地址”的情况下,我想是新的部分)。
如何在Table View Controller中创建类似的行为?
谢谢,丹尼尔
这是一个如何向TableView添加行的示例:
// holding your data
NSMutableArray* data;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [data count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[data objectAtIndex:section] count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//if you want to add a section with one row:
NSMutableArray *row = [[NSMutableArray alloc] init];
[row addObject:@"Some Text"];
[data addObject:row];
[tableView reloadData];
//if you want to add a row in the selected section:
row = [data objectAtIndex:indexPath.section];
[row addObject:@"Some Text"];
[tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [[data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
return cell;
}
Tableview中应该有一个新行。 下一步是用我们自己的数据替换“某些文本”。
这是我要采取的一般方法。
创建一个自定义tableview单元格,并使用类似的方法为其创建委托
-(void)actionButtonTappedInTableViewCell:(UITableViewCell*)cell;
让您的视图控制器成为tableview单元的委托,并在触发该操作时执行以下操作:
-(void)actionButtonTappedInTableViewCell:(UITableViewCell*)cell
{
NSIndexPath *oldIndexPath = [self.tableView indexPathForCell:cell];
NSIndexPath *pathToInsert = [NSIndexPath indexPathForRow:(oldIndexPath.row + 1) inSection:oldIndexPath.section];
[self.tableView beginUpdates];
//now insert with whatever animation you'd like
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:pathToInsert] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
}
将“特殊”行的索引路径添加到数组中,然后在cellForRow方法中检查这是否是特殊行,并进行相应设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.