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);
表视图的移动
- (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);
相关文章推荐
- IOS - 响应者链条
- iOS真机测试-添加测试设备流程
- 加载带有手势识别器的XIB文件需注意哪些问题
- iOS中ScrollView相对布局的使用
- iOS 提示 toast
- iOS入门(二十九)Target Action 和手势
- iOS入门(二十八)事件处理
- ios 动画的时候 如果加阴影 会卡顿的
- ios 8行代码教你搞定导航控制器全屏滑动返回效果
- iOS入门(二十六)iOS程序启动流程
- iOS入门(二十五)基础控件
- 教你轻松看懂 iOS9 新功能
- iOS入门(二十三)内存管理
- iOS入门(二十二)属性
- 更新日志 - 关于 iOS9 设备的安装及其他优化
- iOS开发-- 使用VVDocumenter-Xcode添加代码注释
- iOS入门(二十一)类的扩展
- iOS入门(二十)字典
- iOS研发之静态库的封装
- iOS入门(十九)NSString NSArray NSDictionary