该例子添加UITableView编辑功能
具体功能如下
功能很简单但很实用
@implementation AppDelegate
代码语言:javascript复制@synthesize window = _window;
@synthesize viewController = _viewController;
@synthesize navigationController=_navigationController;
@synthesize array=_array;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.array=[[NSMutableArray alloc] init];
City *city1=[[City alloc] init];
city1.name=@"KunShan";
city1.description=@"Kunshan City,Jiangsu Province China.";
City *city2=[[City alloc] init];
city2.name=@"Shanghai";
city2.description=@"Shanghai City, China.";
City *city3=[[City alloc] init];
city3.name=@"New York";
city3.description=@"the largest city in New York State and in the United States; located in southeastern New York at the mouth of the Hudson river; a major financial and cultural center";
City *city4=[[City alloc] init];
city4.name=@"Tokyo";
city4.description=@"the capital and largest city of Japan; the economic and cultural center of Japan";
[self.array addObject:city1];
[self.array addObject:city2];
[self.array addObject:city3];
[self.array addObject:city4];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.navigationController=[[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
利用appDelegate定义一个nsmutablearrary数据源;应为appDelegate就是一个单例
代码语言:javascript复制- (void)viewDidLoad
{
[super viewDidLoad];
self.title=@"City Guide";
self.navigationItem.rightBarButtonItem=self.editButtonItem;
self.tableView.allowsSelectionDuringEditing=YES;
//self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editTableView)];
AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication] delegate];
array=app.array;
// Do any additional setup after loading the view, typically from a nib.
}
获取数据源
代码语言:javascript复制#pragma mark -uitableviewdelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section{
if ([tv isEditing]) {
return [array count] 1;
}else {
return [array count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell=[tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
// Configure the cell...
if (indexPath.row<array.count) {
City *thisCity=[array objectAtIndex:indexPath.row];
cell.textLabel.text=thisCity.name;
}
else {
cell.textLabel.text=@"Add City";
cell.textLabel.textColor=[UIColor lightGrayColor];
cell.editingAccessoryType=UITableViewCellAccessoryDetailDisclosureButton;
}
return cell;
}
-(void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row< [array count] && !self.editing
) {
//view city detail
CityViewController *cityViewController=[[CityViewController alloc] initWithIndexPath:indexPath];
[self.navigationController pushViewController:cityViewController animated:YES];
}
if (indexPath.row== [array count] && self.editing) {
//add city
}
[tv deselectRowAtIndexPath:indexPath animated:YES];
}
-(void) tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle==UITableViewCellEditingStyleDelete) {
[array removeObjectAtIndex:indexPath.row];
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationLeft];
}
}
-(UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row<[array count]) {
return UITableViewCellEditingStyleDelete;
}else {
return UITableViewCellEditingStyleInsert;
}
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
if (editing!=self.editing) {
[super setEditing:editing animated:animated];
[tableView setEditing:editing animated:animated];
NSMutableArray *indicies=[[NSMutableArray alloc] init];
for (int i=0; i<[array count]; i ) {
[indicies addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
NSArray *lastIndex=[NSArray arrayWithObject:[NSIndexPath indexPathForRow:array.count inSection:0]];
if (editing==YES) {
for (int i=0; i<array.count; i ) {
UITableViewCell *cell=[tableView cellForRowAtIndexPath:[indicies objectAtIndex:i]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
[tableView insertRowsAtIndexPaths:lastIndex withRowAnimation:UITableViewRowAnimationRight];
}else {
for (int i=0; i<array.count; i ) {
UITableViewCell * cell=[tableView cellForRowAtIndexPath:[indicies objectAtIndex:i]];
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
}
[tableView deleteRowsAtIndexPaths:lastIndex withRowAnimation:UITableViewRowAnimationLeft];
}
}
}