您的位置:首页 > 移动开发 > IOS开发

iOS入门(三十四) 表视图的编辑

2015-08-11 16:53 411 查看
表视图的编辑
表视图的移动

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

// Custom initialization

self.nameArr = [[NSMutableArray alloc]initWithObjects:@"龙XX",@"王XX",@"贾XX",@"李XX",@"张XX",@"郑XX",@"马XX",@"王XX",@"马XX",@"尚XX",@"姜XX",@"付XX",@"张XX",@"臧XX",@"付XX",@"裴XX",@"谭XX",@"李XX",@"王XX",@"刘XX", nil];

}

return self;

}

-(void)dealloc

{

[_tableview release];

[_nameArr release];

[super dealloc];

}

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view.

self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 440) style:UITableViewStylePlain];

[self.view addSubview:_tableview];

[_tableview release];

_tableview.delegate = self;

_tableview.dataSource = self;

//1、设置编辑的开关

self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

-(void)setupArr

{

}

//2、重写系统的viewController方法

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

{

[super setEditing:editing animated:animated];

NSLog(@"++++%d",editing);

//利用viewcontroller 的编辑状态,改变TableView的编辑状态

[_tableview setEditing:editing animated:animated];

}

#pragma mark - tableview的代理方法

//3、根据indexPath判断cell能否被编辑

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

{

if (indexPath.row == 12 ) {

return NO;

}

return YES;

}

//4、根据indexPath指定编辑的样式(删除/添加/多选)

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

{

if (indexPath.row == 1 ) {

return UITableViewCellEditingStyleDelete;

}

return UITableViewCellEditingStyleInsert;

}

//5、根据所选的行,执行不同的方法

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

{

if (editingStyle == UITableViewCellEditingStyleDelete ) {

//首先,删除数据源中相应的数据

[self.nameArr removeObjectAtIndex:indexPath.row];

//然后,更新tableView的显示

//方式一:强制TableView重新执行所有的代理方法

// [tableView reloadData];

//方式二:利用TableView的删除方法,删除掉相应的cell

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];

}

if (editingStyle == UITableViewCellEditingStyleInsert) {

[self.nameArr insertObject:@"花花" atIndex:indexPath.row];

[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];

}

}

#pragma mark-tableView 的移动

//判断哪些行可以移动

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

if (indexPath.row == 5 ) {

return NO;

}

return YES;

}

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

[self.nameArr exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];

[tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];

}

-(NSIndexPath*)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

{

NSLog(@"%@====%@",sourceIndexPath,proposedDestinationIndexPath);

//限制cell的跨区移动

if (sourceIndexPath .section != proposedDestinationIndexPath.section) {

return sourceIndexPath;

}

return proposedDestinationIndexPath;

}

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

{

return self.nameArr.count;

}

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

{

static NSString * cellID = @"联系";

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if (cell == nil) {

cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];

}

// cell.imageView.image =

// UIIma[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",indexPath.row +1]];

cell.textLabel.text = [NSString stringWithFormat:@"%@",[_nameArr objectAtIndex:indexPath.row]];

cell.backgroundColor = [UIColor colorWithRed:0.1 green:0.5 blue:0.8 alpha:0.7];

// [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

return cell;

}

NSString * strPath = [[NSBundle mainBundle]pathForResource:@"随便" ofType:@"plist"];

NSDictionary * dic = [NSDictionary dictionaryWithContentsOfFile:strPath];

NSLog(@"%@",dic);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: