IOS UITableView 基础应用

来源:互联网 发布:淘宝定位跳转代码 编辑:程序博客网 时间:2024/04/28 07:21
UITableView 当我们需要手动删除或者添加数据到tableView中的时候 可以使用tableView编辑  (例如 微信中 删除和某人的会话记录)
编辑第一步(TableView方法)
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
编辑第二步(TableView DataSource 方法)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath :(NSIndexPath *)indexPath;
编辑第三步(TableView Delegate 方法)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleFromRowAtIndexPath:(NSIndexPath *)indexPath;
编辑步骤四(TableView DataSource方法)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization        _tableArray = [[NSMutableArray alloc] init];        NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];        [dic1 setObject:@"8.jpg" forKey:@"imageName"];        [dic1 setObject:@"刘喆" forKey:@"name"];        [dic1 setObject:@"男" forKey:@"sex"];//        [_tableArray addObject:dic1];                        NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];        [dic2 setObject:@"8.jpg" forKey:@"imageName"];        [dic2 setObject:@"王键" forKey:@"name"];        [dic2 setObject:@"男" forKey:@"sex"];//        [_tableArray addObject:dic2];                NSMutableDictionary *dic3 = [NSMutableDictionary dictionary];        [dic3 setObject:@"8.jpg" forKey:@"imageName"];        [dic3 setObject:@"王男" forKey:@"name"];        [dic3 setObject:@"男" forKey:@"sex"];//        [_tableArray addObject:dic3];                NSMutableDictionary *dic4 = [NSMutableDictionary dictionary];        [dic4 setObject:@"8.jpg" forKey:@"imageName"];        [dic4 setObject:@"王键男" forKey:@"name"];        [dic4 setObject:@"男" forKey:@"sex"];//        [_tableArray addObject:dic4];                NSMutableArray *array1 = [NSMutableArray array];        [array1 addObject:dic1];        [_tableArray addObject:array1];                NSMutableArray *array2 = [NSMutableArray array];        [array2 addObject:dic2];//数组放到字典中        [_tableArray addObject: array2];    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.    self.navigationController.navigationBar.translucent = NO;    //button按钮   简便的方法    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(editAction:)]autorelease];        //    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];//    [self.view addSubview: button];//    [button release];        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height - 64) style:UITableViewStylePlain];    [_tableview setDelegate:self];    [_tableview setDataSource:self];    [self.view addSubview: _tableview];    [_tableview release];}#pragma mark -#pragma mark table数据编辑//287行 协议UITableViewDataSource-(void)editAction:(id)sender{   // [_tableview setEditing:YES animated:YES];//tableView具有编辑状态,而且有动画效果    //控制编辑状态    if (_tableview.editing == NO) {        [_tableview setEditing:YES animated:YES];    }    else{        [_tableview setEditing:NO animated:NO];    }        }#pragma mark -#pragma mark table数据显示//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{//    if (indexPath.row == 0) {//        return YES;//    }//    return NO;//}//都有多少行- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return [_tableArray count];//返回大数组的个数}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{//    if (section == 0) {//        return [[_tableArray objectAtIndex:0]count];//    }//    else if (section == 1){//        return [[_tableArray objectAtIndex:1]count];////    }     return [[_tableArray objectAtIndex:section]count];//简化后}#pragma mark -   UITableViewCellEditingStyleInsert 添加#pragma mark table编辑样式   UITableViewCellEditingStyleDelete//滑动删除- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{//    return UITableViewCellEditingStyleInsert;     return UITableViewCellEditingStyleDelete;//滑动删除}#pragma mark -#pragma mark table执行操作- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"%s",__func__);    // UITableViewCellEditingStyleDelete,    //UITableViewCellEditingStyleInsert    if (editingStyle == UITableViewCellEditingStyleDelete) {               [_tableArray removeObjectAtIndex:indexPath.row];        [tableView reloadData];    }    else{        NSMutableDictionary *dic = [NSMutableDictionary dictionary];        [dic setObject:@"8.jpg" forKey:@"imageName"];        [dic setObject:@"王键男" forKey:@"name"];        [dic setObject:@"男" forKey:@"sex"];        [_tableArray addObject:dic];        [tableView reloadData];//从新添加一个联系人    }        }#pragma mark -#pragma mark table移动操作 277行7- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{    //为插入做准备 retain自己分配一快空间    id value = [[_tableArray objectAtIndex:sourceIndexPath.row]retain];    NSLog(@"%d",[value retainCount]);//计数    [_tableArray removeObjectAtIndex:sourceIndexPath.row];    NSLog(@"%d",[value retainCount]);    [_tableArray insertObject:value atIndex:destinationIndexPath.row];    NSLog(@"%d",[value retainCount]);    [value release];   }//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{//    return [_tableArray count];//}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *cellIdentify = @"cellidentify";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];    if (!cell) {        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify]autorelease];    }    NSArray *array = [_tableArray objectAtIndex:indexPath.section];    NSDictionary *dic = [array objectAtIndex:indexPath.row];    //取值    //indexPath.row第几行//    NSMutableDictionary *dic = [_tableArray objectAtIndex:indexPath.row];    cell.textLabel.text = [dic objectForKey:@"name"];    cell.detailTextLabel.text = [dic objectForKey:@"sex"];    cell.imageView.image = [UIImage imageNamed:[dic objectForKey:@"imageName"]];    

0 0