iOS之UITableView从新排序

   表格视图在ios 开发中,常用到的视图,几乎每一个app 中多多少少都会有UITableView的影子,就是由于UITableView的功能很是强大,使用起来也很是简单,苹果公司也对接口作了很好的封装,才使用ios程序员这么喜欢它。使用表格视图相关的类UITableViewController,UITableView,UITableViewDataSource,UITableViewDelegate. ios

   咱们能够使用两种方式使用表格,第一种是直接使用UITableViewController,该类是UIViewController的子类;第二种咱们能够使用在UIViewController的view视图中添加UITableView,再继承UITableViewDataSource和UITableViewDelegate 协议。 程序员

   在这里我实现UITableView从新排序,主要使用到UITableDelegate中的两个方法: app

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;

    sample code:

    .h文件 ide

#import <UIKit/UIKit.h>

@interface ReorderViewController : UITableViewController

@end
   .m文件

//
//  ReorderViewController.m
//  ReorderTable
//
//  Created by Carl on 13-6-5.
//  Copyright (c) 2013年 Carl. All rights reserved.
//

#import "ReorderViewController.h"

@interface ReorderViewController ()
@property (nonatomic,strong) NSMutableArray * dataSource;

@end

@implementation ReorderViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.title = @"Recordering Rows";
    self.dataSource = [[NSMutableArray alloc] initWithObjects:@"Drag to reorder 1 ",@"Drag to reorder 2 ",@"Drag to reorder 3 ",@"Drag to reorder 4 ",@"Drag to reorder 5 ",@"Drag to reorder 6 ", nil];
//    self.editing = YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [_dataSource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    cell.textLabel.text = [_dataSource objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    // Configure the cell...
    
    return cell;
}


/*
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}


-(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}
*/
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    [_dataSource exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
}



// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}




#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end
     效果图:


  若是想在edit状态取消delete按钮,须要实现如下两个方法: this

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}


-(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}
  若是想一装入视图就显示move按钮,须要在viewDidLoad中添加如下代码

self.editing = YES;
效果图:

相关文章
相关标签/搜索