iOS攻略之动态添加tableView的行

方法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)];

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值