IOS-UITableView編輯模式樣本
概要
本樣本實在上篇文章的基礎上的例子修改過來的,主要是簡示了UITableView的編輯模式的使用,包括狀態改變、移動行、刪除行。
運行結果
過程概要
見代碼及注釋,不難
主要代碼h檔案
//// CityViewController.h// NatTab//// Created by God Lin on 14/12/7.// Copyright (c) 2014年 arbboter. All rights reserved.//#import @interface CityViewController : UIViewController { NSMutableArray* _arrayName; UITableView* _tableView;}@property (nonatomic, retain) NSMutableArray* _arrayName;@property (nonatomic, retain) UITableView* _tableView;@end
m檔案
//// CityViewController.m// NatTab//// Created by God Lin on 14/12/7.// Copyright (c) 2014年 arbboter. All rights reserved.//#import "CityViewController.h"@interface CityViewController ()@end@implementation CityViewController@synthesize _arrayName;@synthesize _tableView;#pragma UITableViewDelegate- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleDelete;}#pragma UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self._arrayName count];}// 插入刪除- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleDelete) { [self._arrayName removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view }}// 移動- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ NSString* fromObj = [self._arrayName objectAtIndex:sourceIndexPath.row]; [self._arrayName insertObject:fromObj atIndex:destinationIndexPath.row]; [self._arrayName removeObjectAtIndex:sourceIndexPath.row];}- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ return YES;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell* cell = nil; static NSString *CellIdentifier = @"Cell"; cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } NSString* iconStr = [NSString stringWithFormat:@"animal_%u.png", arc4random()%17+1]; cell.imageView.image = [UIImage imageNamed:iconStr]; cell.textLabel.text = [self._arrayName objectAtIndex:indexPath.row]; return cell;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}- (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; CGRect viewRect = self.view.frame; self._tableView = [[UITableView alloc] initWithFrame:viewRect]; self._tableView.delegate = (id)self; self._tableView.dataSource = self; [self.view addSubview:self._tableView]; self._arrayName = [[NSMutableArray alloc] init];}- (void)setEditing:(BOOL)editing animated:(BOOL)animated{ [super setEditing:editing animated:animated]; [self._tableView setEditing:editing animated:animated];}- (void)viewWillAppear:(BOOL)animated{ int nRow = arc4random()%50+20; NSString* str = nil; [self._arrayName removeAllObjects]; for (int i=0; i
工程代碼