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;
效果圖:

相關文章
相關標籤/搜索