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
  • 相关阅读:
    linux文件锁flock【转】
    无尽的悲伤
    go的临时对象池--sync.Pool
    golang 小知识-持续更新中
    【转】Go Channels
    Golang文件名命名规则
    Parquet存储格式
    预装的Office2016,文件图标表显示以及新建失败问题解决 方法
    Gamma编码及Delta编码概述
    java web开发环境配置系列(二)安装tomcat
  • 原文地址:https://www.cnblogs.com/letougaozao/p/3652927.html
Copyright © 2011-2022 走看看