//
// MainViewController.m
// UI10_tableview的编辑
//
// Created by dllo on 15/8/11.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "MainViewController.h"
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *arr;
@end
@implementation MainViewController
- (void)dealloc
{
[_tableView release];
[_arr release];
[super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.translucent = NO;
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
[_tableView release];
self.tableView.delegate = self;
self.tableView.dataSource = self;
// editButtonItem系统自带的编辑按钮
self.navigationItem.rightBarButtonItem = self.editButtonItem;
// 直接打开tableview的可编辑模式
// [self.tableView setEditing:YES animated:YES];
}
#pragma mark 重写系统的编辑按钮点击触发的方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:YES];
}
#pragma mark 设置哪些行可以进行编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// 奇数行可以编辑,偶数行不能编辑
// if (indexPath.row % 2 == 1) {
// return YES;
// } else {
// return NO;
// }
// 默认是YES
return YES;
}
// 有两种样式,一个是插入,一个是删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
// 删除数据,提供了一个左划可以编辑的效果
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 先删除数据源
[self.arr removeObjectAtIndex:indexPath.row];
// [self.tableView reloadData];
// 通过tableview来删除上面的cell
// 第一个参数:指定删除哪一个分区的哪个行,把他作为一个元素放在数组中
// 第二个参数:删除动画
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
// 修改删除按钮的标题
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"点击一下";
}
#pragma mark 这个方法是iOS8.0之后出现的方法,可以在编辑状态的时候有多个按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// 按钮的点击所要触发的事件,都是写在block中
NSLog(@"触发了删除按钮");
}];
UITableViewRowAction *topAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"置顶");
}];
deleteAction.backgroundColor = [UIColor lightGrayColor];
topAction.backgroundColor = [UIColor orangeColor];
return @[deleteAction, topAction];
}
// 移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 1.先获取到起始位置的数据,retain为了防止引用计算变为1时直接执行delloac方法
NSString *str = [self.arr[sourceIndexPath.row] retain];
// 2.把起始位置的对象从数据源中移除
[self.arr removeObjectAtIndex:sourceIndexPath.row];
// 3.把数据插入到数组的目的地位置上
[self.arr insertObject:str atIndex:destinationIndexPath.row];
[str release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
}
cell.textLabel.text = self.arr[indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
UI10_tableview的编辑
最新推荐文章于 2016-01-19 19:15:27 发布