iOS之UITableView重新排序
表格視圖在ios開發(fā)中,經(jīng)常使用到的視圖,幾乎每個app 中多多少少都會有UITableView的影子,就是因為UITableView的功能非常強(qiáng)大,使用起來也非常簡單,蘋果公司也對接口做了很好的封裝, 才使用ios程序員這么喜歡它。使用表格視圖相關(guān)的類 UITableViewController,UITableView,UITableViewDataSource,UITableViewDelegate.
我們可以使用兩種方式使用表格,第一種是直接使用UITableViewController,該類是UIViewController的子類;第二種 我們可以使用在UIViewController的view視圖中添加UITableView,再繼承UITableViewDataSource和 UITableViewDelegate 協(xié)議。
在這里我實現(xiàn)UITableView重新排序,主要使用到UITableDelegate中的兩個方法:
- - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
- - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;
sample code:
.h文件
- #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;
- }
- <span style="font-size:9pt;line-height:1.5;">*/</span>
- // 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狀態(tài)取消delete按鈕,需要實現(xiàn)以下兩個方法:
- -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return UITableViewCellEditingStyleNone;
- }
- -(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return NO;
- }
如果想一裝入視圖就顯示move按鈕,需要在viewDidLoad中添加以下代碼
- self.editing = YES;
效果圖: