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

    }

  • 相关阅读:
    IIS部署网站 HTTP 错误 500.21
    Oracle常用操作表空间sql脚本
    oracle 11g错误ora-01033:oracle initialization or shutdown in progress解决办法
    关于OPC连接读写下位机PLC(转)
    oracle 日期格式操作
    第三方app抽奖发送微信红包实现
    XML 字符串解析
    VS2013 统计代码量(使用正则表达式)
    使用IntelliJ IDEA 配置Maven(入门)(转)
    学习方法分享 | 从JVM说起,聊聊Java的学习和复习!
  • 原文地址:https://www.cnblogs.com/neozhu/p/2482447.html
Copyright © 2020-2023  润新知