前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ios5开发-UITableView开启编辑功能

ios5开发-UITableView开启编辑功能

作者头像
阿新
发布2018-04-12 15:19:27
7770
发布2018-04-12 15:19:27
举报
文章被收录于专栏:c#开发者c#开发者

该例子添加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];
        }
    }

}

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-05-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档