UITabView 添加

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

{

    UITableView *_tableView;

    NSMutableArray *_arrayM;

}

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    [self getData];

    [self addUITableView];

}

- (void)getData{

    _arrayM = [NSMutableArray array];

    for (int i = 0; i < 10; i ++) {

        NSString *str = [NSString stringWithFormat:@"我是第%d行",i];

        [_arrayM addObject:str];

    }

}

- (void)addUITableView{

    _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

    _tableView.delegate = self;

    _tableView.dataSource = self;

    [self.view addSubview:_tableView];

}

#pragma mark - 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _arrayM.count;

}

- (UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];

    }

    cell.textLabel.text = _arrayM[indexPath.row];

    cell.detailTextLabel.text = _arrayM[indexPath.row];

    return cell;

}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{

 //1.重写按钮编辑点击事件  首先要调用父类方法

    [super setEditing:editing animated:YES];

//2.打开UItableview的编辑模式

    [_tableView setEditing:editing animated:YES];

    

}

 //3.允许row编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;

}

//4.设置编辑样式

- (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    return UITableViewCellEditingStyleInsert;

}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *str = @"我是外来户";

    if (editingStyle ==  UITableViewCellEditingStyleInsert) {

        //1.放入数据源

        [_arrayM insertObject:str atIndex:indexPath.row];

        //2.chu

        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    }

}

原文地址:https://www.cnblogs.com/fanwenzheIOS/p/4973809.html