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
  • 相关阅读:
    Asp.Net细节性问题技巧精萃
    存储过程(Stored Procedure)及应用
    合并datagrid中内容相同的单元格
    .net 2.0 下发送邮件的方式
    ADO.NET2.0的十大新特性
    sql server 中各个系统表的作用
    DataGrid一些文章的索引,方便查找
    ASP.NET中 WebControls 命名规则
    SQL Server应用程序中的高级SQL注入[转]
    数据操作例子
  • 原文地址:https://www.cnblogs.com/letougaozao/p/3652927.html
Copyright © 2011-2022 走看看