应用程序之UITableView的编辑模式

  • cell分层结构
  • 效果展示
  • 代码实现

一、cell的分层结构

二、效果展示

三、代码实现

//
//  ViewController.m
//  01-TableView的删除实现
//
//  Created by apple on 14-4-8.
//  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"

@interface ViewController () <UITabBarDelegate, UITableViewDataSource>
{
    NSMutableArray *persons;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    persons = [NSMutableArray array];
    for (int i = 0; i<30; i++) {
        Person *p = [Person personWithName:[NSString stringWithFormat:@"person--%d", i] phone:[NSString stringWithFormat:@"%d", 10000 + arc4random_uniform(1000000)]];
        [persons addObject:p];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return persons.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    }
    
    Person *person = persons[indexPath.row];
    
    cell.textLabel.text = person.name;
    cell.detailTextLabel.text = person.phone;
    
    return cell;
}

#pragma mark -删除方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle != UITableViewCellEditingStyleDelete) return;
    
    [persons removeObjectAtIndex:indexPath.row];
    
    [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}

#pragma mark -拖动方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    Person *p = persons[sourceIndexPath.row];
    [persons removeObjectAtIndex:sourceIndexPath.row];
    [persons insertObject:p atIndex:destinationIndexPath.row];
}

- (IBAction)remove:(id)sender {
    BOOL result = !_tableView.editing;
    [_tableView setEditing:result animated:YES];
}
@end

 

 

posted @ 2014-04-08 21:08  了透糕糟  阅读(156)  评论(0编辑  收藏  举报