zoukankan      html  css  js  c++  java
  • 应用程序之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
  • 相关阅读:
    招行面试
    今日头条面试[教育岗]
    四方精创 面试
    ArrayList 源码
    redis缓存,穿透,击穿,雪崩
    hashMap
    集合整理
    阿里CBU技术部一面
    网安面试
    php递归获取顶级父类id
  • 原文地址:https://www.cnblogs.com/letougaozao/p/3652927.html
Copyright © 2011-2022 走看看