方法1:添加一个按钮实现动态添加tableView的行
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.dataArr = [NSMutableArray arrayWithObjects:@"我是初代", nil];
self.testTV.delegate = self;
self.testTV.dataSource = self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
cell.textLabel.text = self.dataArr[indexPath.row];
return cell;
}
- (IBAction)addBtn:(UIButton *)sender {
// 插入多行(插入了三行cell)
// NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
// for (int i = 0; i < 3; i++) {
// NSString *s = [[NSString alloc] initWithFormat:@"包子入侵%ld号",self.dataArr.count];
// [self.dataArr addObject:s];
// NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.dataArr.count - 1 inSection:0];
// [indexPaths addObject:indexPath];
// }
// 插入单行(插入了一行cell)
NSString *s = [[NSString alloc] initWithFormat:@"包子入侵%ld号",self.dataArr.count];
[self.dataArr addObject:s];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.dataArr.count - 1 inSection:0];
[self.testTV beginUpdates];
[self.testTV insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; // 插入方法
[self.testTV endUpdates];
// 滚动到最后一行
[self.testTV selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.dataArr.count-1 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionBottom];
}
当然也可以通过
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
代理方法实现插入到指定位置
模拟器截图:
方法二: 通过tableView的可编辑方法实现插入或者删除
// 让 UITableView 和 UIViewController 变成可编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[_tableView setEditing:editing animated:animated];
}
// 指定哪一行可以编辑 哪行不能编辑
- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// 设置 哪一行的编辑按钮 状态 指定编辑样式
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return _editingStyle;
}
// 判断点击按钮的样式 来去做添加 或删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 删除的操作
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_titleArray removeObjectAtIndex:indexPath.row];
[_icoArray removeObjectAtIndex:indexPath.row];
NSArray *indexPaths = @[indexPath]; // 构建 索引处的行数 的数组
// 删除 索引的方法 后面是动画样式
[_tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:(UITableViewRowAnimationLeft)];
}
// 添加的操作
if (editingStyle == UITableViewCellEditingStyleInsert) {
NSArray *indexPaths = @[indexPath];
[_tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:(UITableViewRowAnimationRight)];
}
}