您的位置:首页 > 产品设计 > UI/UE

UI学习笔记---第十天UITableView表视图编辑

2014-07-22 22:48 429 查看
UITableView表视图编辑

表视图编辑的使用场景

当我们需要手动添加或者删除某条数据到tableView中的时候,就可以使用tableView编辑.比如微信 扣扣中删除和某人的通话

当我们需要手动调整单元格的顺序时,就可以通过tableView移动,移动单元格到指定位置

代理AppDelegate.m中代码

#import "AppDelegate.h"
#import "RootViewController.h"
@implementation AppDelegate
-(void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];

RootViewController *rootVC = [[RootViewController alloc] init];
UINavigationController *ngVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = ngVC;

[ngVC release];
[rootVC release];

[self.window makeKeyAndVisible];
return YES;
}


RootViewController.h中建立一个可变数组属性

NSMutableArray *_mArr;

RootViewController.m中初始化和loadView代码

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.navigationItem.title = @"百家讲坛";
self.navigationItem.rightBarButtonItem = self.editButtonItem;//控制器自带的编辑按钮
_mArr = [[NSMutableArray alloc] initWithObjects:@"赵",@"钱",@"孙",@"李",@"周",@"武",@"郑",@"王",@"唐僧",@"孙悟空",@"猪八戒",@"沙僧",@"小白龙",@"二郎神",@"哪吒",@"雷震子", nil];
// Custom initialization
}
return self;
}
-(void)loadView
{

UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStyleGrouped];
table.dataSource = self;
table.delegate = self;
self.view = table;
[table release];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_mArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [_mArr objectAtIndex:indexPath.row];
//    tableView.editing = YES;
//cell右侧属性
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}


编辑的步骤

1.让tableView处于编辑状态

-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
//调用父类方法,实现edit和done的变换
[super setEditing:editing animated:animated];
UITableView *tableView = (UITableView *)self.view;
[tableView setEditing:editing animated:animated];
}


2.指定tableView那些行可以编辑

//设置cell的可编辑状态,默认是yes
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.row == 0) {
//        return YES;
//    }
return YES;
}


3.指定tableView的编辑的样式(添加.删除)

//delegate中得方法
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row ==0) {
return UITableViewCellEditingStyleInsert;//添加
}
return UITableViewCellEditingStyleDelete;//删除
}


4.编辑完成(先操作数据源,后修改UI)

//点击加号或者delete时触发的事件
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
//删除数据  (写在删除cell前面  或者写一个[tableView beginUpdates]放前面一个[tableView endUpdates]放后面)
[_mArr removeObjectAtIndex:indexPath.row];
//删除cell
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
[tableView endUpdates];
NSLog(@"删除");
}else{
[tableView beginUpdates];
[_mArr insertObject:@"hello" atIndex:indexPath.row];
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
[tableView endUpdates];

NSLog(@"添加");
}
}


表视图的移动

移动的步骤

1.让tableView处于编辑状态

-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
//调用父类方法,实现edit和done的变换
[super setEditing:editing animated:animated];
UITableView *tableView = (UITableView *)self.view;
[tableView setEditing:editing animated:animated];
}


2.指定tableView哪些行可以移动

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}


3.移动完成

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSString *str = [_mArr objectAtIndex:sourceIndexPath.row];

//引用计数加1.避免出现野指针
[str retain];

//删除元素
[_mArr removeObjectAtIndex:sourceIndexPath.row];
//插入元素
[_mArr insertObject:str atIndex:destinationIndexPath.row];

[str release];//释放之前,retain的对象
}


监测移动过程,实现限制跨区移动

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
NSLog(@"%d",sourceIndexPath.row);
NSLog(@"%d",proposedDestinationIndexPath.row);
if (sourceIndexPath.row == [_mArr count]-1) {
return sourceIndexPath;
}else{
return proposedDestinationIndexPath;
}
}


UITableViewController表视图控制器

继承自UIViewController

自带一个tableView,根视图就是tableView

模板自带编辑移动相关的代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐