zoukankan      html  css  js  c++  java
  • ios5开发UITableView开启编辑功能

    该例子添加UITableView编辑功能

    具体功能如下

     

    功能很简单但很实用 

     @implementation AppDelegate


    @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就是一个单例

    - (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.

     获取数据源

    #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];
            }
        }

    }

  • 相关阅读:
    html的转码玉反转码
    获取url据对路径写法
    CSS 外边距合并
    页面禁制选中元素的 背景变蓝的通用写法
    centos7.3上安装oracle11.2.4RAC
    通过ansible检查所有服务器根目录磁盘使用情况
    解决es集群启动完成后报master_not_discovered_exception(hostname有错误)
    tidb4.0执行大型sql报没有tmp目录错处理(ERROR 1105 (HY000): open /tmp/1000_tidb/MC4wLjAuMDo0MDAwLzAuMC4wLjA6MTAwODA)
    aix磁盘创建pv、lv
    aix6.1安装oracle
  • 原文地址:https://www.cnblogs.com/neozhu/p/2482447.html
Copyright © 2011-2022 走看看