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

iOS table简单实现增加移动和删除功能与自定义cell的实现

2014-03-30 18:26 746 查看
在RootViewController.h文件中

#import <UIKit/UIKit.h>

@interface RootViewController :UIViewController
{
BOOL editState;
}

//@property (nonatomic,retain)NSArray *noteList;

@property (nonatomic,retain)NSMutableArray *noteList;

@property (weak,
nonatomic) IBOutletUITableView *mTableView;

//添加移动操作
- (void)noteMove;

//添加删除
- (void)noteDelete:(id)sender;

@end

在RootViewController.m

#import "RootViewController.h"

@interface
RootViewController () <UITableViewDataSource,UITableViewDelegate>

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
self.title =@"简单的表";
}

return
self;
}

- (void)viewDidLoad
{

[superviewDidLoad];

NSMutableArray *array = [[NSMutableArrayalloc]initWithObjects:@"2009-12-1",@"2009-12-2",@"2009-12-3",@"2009-12-4",@"2009-12-5",@"2009-12-6",nil];
self.noteList = array;



UIBarButtonItem *moveButton = [[UIBarButtonItemalloc]initWithTitle:@"Move"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(noteMove)];

self.navigationItem.rightBarButtonItem = moveButton;



UIBarButtonItem *deleteButton = [[UIBarButtonItemalloc]initWithTitle:@"delete"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(noteDelete:)];

self.navigationItem.leftBarButtonItem = deleteButton;
}

//添加行的移动
- (void)noteMove
{

editState =
YES;

[self.mTableViewsetEditing:!self.mTableView.editinganimated:YES];
}

//添加行的删除
- (void)noteDelete:(id)sender
{

editState =NO;

[self.mTableViewsetEditing:!self.mTableView.editinganimated:YES];
}

//返回指定分期的行数分区默认是1个
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.noteListcount];
}

//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static
NSString *NoteScanIdentifier =@"NoteScanIdentifier";
UITableViewCell *cell =[tableView
dequeueReusableCellWithIdentifier:NoteScanIdentifier];

//这里使用NoteScanIdentifer类型的可重用单元检查一下单元是否为空(nil),如果是,就要使用前面所提到的标识符字符串来创建一个新的表视图单元。
if(cell ==
nil)
{

cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:NoteScanIdentifier];

cell.showsReorderControl =YES;//为行指定了标准扩展图标,只有表处于编辑时才可以显示,通过设置表视图单元的这个属性,才能启动移动控件,可以进入移动的状态。
}
NSUInteger row = [indexPath
row];
cell.textLabel.text = [_noteListobjectAtIndex:row];
UIImage *image = [UIImageimageNamed:@"aiside.png"];
cell.imageView.image = image;
return cell;
}

/*行的移动方法*/

//确认编辑类型
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath
*)indexPath
{

if(!editState)//是否处于编辑状态

returnUITableViewCellEditingStyleDelete;
else

returnUITableViewCellEditingStyleNone;
}

//指定该行能够移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath
*)indexPath
{

if(editState)
return
YES;
else

returnNO;
//如果点了删除行不能移动
}

//移动方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath
*)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSUInteger fromRow = [sourceIndexPath
row]; //要移动的位置
NSUInteger toRow = [destinationIndexPath
row]; //移动的目的位置
id object = [_noteListobjectAtIndex:fromRow];
//存储将要被移动的位置的对象
[_noteListremoveObjectAtIndex:fromRow];
//将对象从原位置移除
[_noteListinsertObject:object
atIndex:toRow]; //将对象插入到新位置
}

//删除cell方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath
row]; //获取当前行

[self.noteListremoveObjectAtIndex:row];
//在数据中删除当前对象

[_mTableViewdeleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];//数组执行删除操作
}

@end

运行结果









自定义cell

xib文件



CustomCell.h文件为:

#import <UIKit/UIKit.h>

@interface CustomCell :
UITableViewCell

@property (weak,
nonatomic) IBOutlet
UILabel *dateLabel;

@property (weak,
nonatomic) IBOutlet
UILabel *titleLabel;

@end

RootViewController.m改为

//

// RootViewController.m

// 实现一个简单的表

//

// Created by qin on 14-3-30.

// Copyright (c) 2014年 zhou. All rights reserved.

//

#import "RootViewController.h"

#import "NoteDetailController.h"

#import "CustomCell.h"

#import "pxAppDelegate.h"

@interface
RootViewController () <UITableViewDataSource,UITableViewDelegate>

@end

@implementation RootViewController

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

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

if (self) {

self.title =
@"简单的表";
}

return
self;
}

- (void)viewDidLoad
{

[super
viewDidLoad];

self.title =
@"Note Scan";

NSDictionary *row1 = [[NSDictionary
alloc]initWithObjectsAndKeys:@"2009-12-01",@"Date",@"学习
object-c",@"Title",
nil];

NSDictionary *row2 = [[NSDictionary
alloc]initWithObjectsAndKeys:@"2009-12-02",@"Date",@"学习
Cocoa",@"Title",
nil];

NSDictionary *row3 = [[NSDictionary
alloc]initWithObjectsAndKeys:@"2009-12-03",@"Date",@"学习
iphone开发",@"Title",
nil];

NSDictionary *row4 = [[NSDictionary
alloc]initWithObjectsAndKeys:@"2009-12-04",@"Date",@"项目需求分析",@"Title",
nil];

NSDictionary *row5 = [[NSDictionary
alloc]initWithObjectsAndKeys:@"2009-12-05",@"Date",@"项目讨论",@"Title",
nil];

NSDictionary *row6 = [[NSDictionary
alloc]initWithObjectsAndKeys:@"2009-12-06",@"Date",@"项目开发",@"Title",
nil];

NSArray *array = [[NSArray
alloc]initWithObjects:row1,row2,row3,row4,row5,row6 ,nil];



self.noteList = array;
}

//添加行的移动
- (void)noteMove
{

editState =
YES;

[self.mTableView
setEditing:!self.mTableView.editing
animated:YES];
}

//添加行的删除
- (void)noteDelete:(id)sender
{

editState =NO;

[self.mTableView
setEditing:!self.mTableView.editing
animated:YES];
}

//返回指定分期的行数
分区默认是1个
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return [self.noteList
count];
}

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

static NSString *CustomCellIdentifier =
@"CustomCellIdentifier";

//自定义cell

CustomCell *cell = (CustomCell *)[tableView
dequeueReusableCellWithIdentifier:CustomCellIdentifier];



//这里使用NoteScanIdentifer类型的可重用单元
检查一下单元是否为空(nil),如果是,就要使用前面所提到的标识符字符串来创建一个新的表视图单元。

if(cell == nil)
{

NSArray *nib = [[NSBundle
mainBundle]loadNibNamed:@"CustomCell"
owner:self
options:nil];
cell = [nib
objectAtIndex:0];
//把xib中的第一个对象赋给cell
}

NSUInteger row = [indexPath
row];

NSDictionary *rowdata = [self.noteList
objectAtIndex:row];
cell.dateLabel.text = [rowdata
objectForKey:@"Date"];
cell.titleLabel.text = [rowdata
objectForKey:@"Title"];

[cell setAccessoryType:UITableViewCellAccessoryDetailButton];



return cell;
}

/*行的移动方法*/

//确认编辑类型
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath
*)indexPath
{

if(!editState)
//是否处于编辑状态

return
UITableViewCellEditingStyleDelete;

else

return
UITableViewCellEditingStyleNone;
}

//指定该行能够移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath
*)indexPath
{

if(editState)

return YES;

else

return
NO;
//如果点了删除行不能移动
}

//移动方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath
*)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{

NSUInteger fromRow = [sourceIndexPath
row]; //要移动的位置

NSUInteger toRow = [destinationIndexPath
row]; //移动的目的位置

id object = [_noteList
objectAtIndex:fromRow];
//存储将要被移动的位置的对象
[_noteList
removeObjectAtIndex:fromRow];
//将对象从原位置移除
[_noteList
insertObject:object
atIndex:toRow]; //将对象插入到新位置
}

//删除cell方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{

NSUInteger row = [indexPath
row]; //获取当前行

[self.noteList
removeObjectAtIndex:row]; //在数据中删除当前对象

[_mTableView
deleteRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//数组执行删除
操作
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{

UIAlertView *alert = [[UIAlertView
alloc]initWithTitle:@"查看详细信息" message:@"如果要查看详细信息请点击蓝色按钮"
delegate:nil cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert
show];
}

//点击公开按钮后触发的操作
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath
*)indexPath
{

NSLog(@"点击了蓝色按钮!!");

if(subController ==
nil)
{

subController = [[NoteDetailController
alloc]initWithNibName:@"NoteDetailController"
bundle:nil];
}

subController.title =
@"详细信息";

NSUInteger row = [indexPath
row];



NSDictionary *seletedDate = [_noteList
objectAtIndex:row];

NSString *selectStr = [seletedDate
objectForKey:@"Date"];

NSString *titleStr = [seletedDate
objectForKey:@"Title"];

NSString *detailMessage = [[NSString
alloc]initWithFormat:@"详细内容:%@---->%@",selectStr,titleStr];

subController.message = detailMessage;



// UINavigationController *nav = [[UINavigationController alloc]init];

// [nav pushViewController:subController animated:YES];

pxAppDelegate *delegate = [[UIApplication
sharedApplication]delegate];

[delegate.nav
pushViewController:subController
animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath
{

return
60;
//cell的高度


}

@end

运行结果:




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