iOS学习笔记——表视图二(编辑)

表视图的可编辑属性,即对表的每一个单元UITableViewCell进行操作的。总共有5种方法,其中4种方法在UITableViewDataSource协议里,最后一种方法在UITableViewDelegate协议中。

在.h文件中添加协议,创建对象:

@interface LinViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
//创建表视图对象
@property (retain, nonatomic) UITableView *mTableView;
//创建数组对象,存储表视图数据
@property (retain, nonatomic) NSMutableArray *mArray;

@end
在.m文件里添加实现的方法:

@implementation LinViewController

//重写初始化方法,原方法在AppDeledate.m文件中
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        //设置导航栏的名称
        self.navigationItem.title = @"首页";
        //设置导航栏的右按钮,并关联方法
        UIBarButtonItem *pButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"BeginEdit" style:UIBarButtonItemStylePlain target:self action:@selector(tableEdit:)];
        //把按钮添加到导航栏上
        self.navigationItem.rightBarButtonItem = pButtonItem;
    }
    return self;
}
//导航栏上按钮对应的方法,实现名称的切换
- (void)tableEdit:(id)sender
{
    //三目运算,根据表视图是否可编辑的状态设置按钮的标题
    [sender setTitle:[self.mTableView isEditing]?@"BeginEdit":@"EndEdit"];
    //设置表视图的编辑状态,在可编辑与不可编辑之间切换
    [self.mTableView setEditing:![self.mTableView isEditing]];
}
//释放创建的对象
- (void)dealloc
{
    [_mTableView release];
    [_mArray release];
    [super dealloc];
}
//视图的加载
- (void)viewDidLoad
{
    [super viewDidLoad];
    //初始化表视图对象
	self.mTableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    //设置委托对象,表视图的委托
    self.mTableView.dataSource = self;
    self.mTableView.delegate = self;
    //把表视图添加到当前的视图中
    [self.view addSubview:self.mTableView];
    //初始化数组对象
    self.mArray = [NSMutableArray arrayWithCapacity:5];
    //给数组中元素设置为字符串类型
    for (int i = 0; i < 5; i++)
    {
        NSString *pStr = [NSString stringWithFormat:@"%d",i];
        [self.mArray addObject:pStr];
    }
}
#pragma mark---UITableVeiwDataSource---表视图的协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //获取表视图中需要加载表的行数
    return [self.mArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //设置一个静态字符串做标签,(静态防止局部变量多次创建,提高效率,节约内存)
    static NSString * identifer = @"identifer";
    //创建一个cell对象(利用表示图可以复用的机制)
    UITableViewCell * Cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    //如果cell为空,就创建一个新的出来
    if (nil == Cell)
    {
        Cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];
    }
    //获取当前表视图行数
    NSInteger row = [indexPath row];
    //把数组中对应元素传递给视图的文本
    Cell.textLabel.text = [self.mArray objectAtIndex:row];
    return Cell;
}
//设置表的编辑属性,5种方法
//设置当前title是否可编辑属性,第一个方法
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == 0)
    {
        return NO;
    }
    return YES;
}
#pragma mark---UITableViewDelegate协议的方法,判断编辑方式,此处只是用这个方法
//设置表视图的编辑方式,包括删除与插入,第二个方法
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row]%2)
    {
        return UITableViewCellEditingStyleInsert;
    }
    return UITableViewCellEditingStyleDelete;
}
//确定表视图的编辑方式,第三个方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //判断编辑方式为删除
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        //通过行数移除数组中元素
        [self.mArray removeObjectAtIndex:[indexPath row]];
        //表视图开始更新
        [self.mTableView beginUpdates];
        //获取数组元素,在表视图中删除
        [self.mTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        //表视图终止更新
        [self.mTableView endUpdates];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        //在数组对应行数的位置添加新元素
        [self.mArray insertObject:@"New Cell" atIndex:[indexPath row]];
        //表视图开始更新
        [self.mTableView beginUpdates];
        //获取数组元素,在表视图中插入
        [self.mTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
        //表视图终止更新
        [self.mTableView endUpdates];
    }
}
//设置表视图中行数的可以动属性,第四个方法
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == 2)
    {
        return NO;
    }
    return YES;
}
//编辑表视图的移动方式第五个方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //获取移动前的行
    NSInteger fromRow = [sourceIndexPath row];
    //获取要移动到的行
    NSInteger toRow = [destinationIndexPath row];
    //获取数组中移动前下标对应的对象
    id object = [self.mArray objectAtIndex:fromRow];
    //从数组中删除
    [self.mArray removeObjectAtIndex:fromRow];
    //在新的位置插入
    [self.mArray insertObject:object atIndex:toRow];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值