TableView編輯中實現多行刪除的2中方法以及注意,tableview多行
在tableView最後的學習編輯中如何?刪除,增加,移動一行都是通過代理方法實現的,對於新手注意的是在實行編輯的時候一定要注意根資料一定要和tablView中的資料保持一致性。因為每次修改tableView的時候他都會重新整理tableView中得資料;總之刪一個數組的資料自後在刪除tableView的一行;
額在ios5.0之後有:
表視圖增加新的功能多選,他的預設是NO self.tableView.allowsMultipleSelectionDuringEditing=YES;他一般寫作設定tableView編輯的風格之上。
//選中後的cell索引集合 NSArray *indexpath=self.tableView.indexPathsForSelectedRows; 有了這兩種方法就可以很大的程度上減小了邏輯的複雜度:
//先建立一個UIBarButtonItrm的執行個體,並且要實現一個響應的事件方法
UIBarButtonItem *barItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(Cancel)];
self.navigationItem.leftBarButtonItem=barItem;
-(void)Cancel{
//把使用者打鉤的行數的索引集合放到一個數組中
NSArray *indexpath=self.tableView.indexPathsForSelectedRows;
NSLog(@"%@",indexpath);
for (int i=0; i<indexpath.count; i++) {
//遍曆這個數組並把每個元素都取出來
NSNumber *number=[mulArray objectAtIndex:i];
int a=[number intValue];
NSString *str=[mulArray objectAtIndex:a];
然後在遍曆出的元素在數組中刪除。這就是我們上面說的要想編輯tableView就的先改變根資料
[mulArray removeObject:str];
}
如果在ios5.0之前的話就得用網上的方法,額我就先說一個在發表自己的代碼一定要增加可閱讀性或者寫注釋額,這些代碼讓我這些初學者費了不少的力
其實用了一個組合的代理方法#import "UITableViewDelteMutilRowsViewController.h"
@implementation UITableViewDelteMutilRowsViewController@synthesize tableview;@synthesize dataArray;@synthesize deleteDic;@synthesize leftButton;@synthesize rightButton;#pragma mark -#pragma mark View lifecycle- (void)viewDidLoad { [super viewDidLoad];
//建立一個跟資料,和一個字典,並把自定的按鈕命名 dataArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",nil]; deleteDic = [[NSMutableDictionary alloc] init]; rightButton.title = @"編輯";}- (IBAction)choseData{
//點擊這個按鈕的時候觸發判斷是否處於編輯狀態如果不是修改按鈕的名字開啟編輯狀態。反之也是 if (rightButton.title == @"編輯") { rightButton.title = @"確定"; [self.tableview setEditing:YES animated:YES]; } else { rightButton.title = @"編輯"; [deleteDic removeAllObjects]; [self.tableview setEditing:NO animated:YES]; }}- (IBAction)deleteFuntion{
//這個一個全部刪除全部資料的按鈕,把使用者點擊的資料從數組上和tableView上刪除。
[dataArray removeObjectsInArray:[deleteDic allKeys]]; [self.tableview deleteRowsAtIndexPaths:[NSArray arrayWithArray:[deleteDic allValues]] withRowAnimation:UITableViewRowAnimationFade]; [deleteDic removeAllObjects]; }- (void)dealloc { [leftButton release]; [rightButton release]; [deleteDic release]; [dataArray release]; [tableview release]; [super dealloc];}#pragma mark -#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 [dataArray count];}// Customize the appearance of table view cells.- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell... cell.textLabel.text = [dataArray objectAtIndex:indexPath.row]; return cell;}/*//這裡設定為可滑動編輯刪除 // Override to support conditional editing of the table view. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable. return YES; } */- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;}#pragma mark -#pragma mark Table view delegate//這倆個代理方法是組合的使用當使用者點擊tableView時觸發,而這個字典那個是可以可以把使用者點擊的行數在數組的根資料中提取出來,並且把他賦予字典的key的值,路徑為Value值- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (rightButton.title== @"確定") { [deleteDic setObject:indexPath forKey:[dataArray objectAtIndex:indexPath.row]]; } else { }}//這個方法是取消編輯時調用,把所對應字典的key值刪除。這是取消選中的。- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ if (rightButton.title == @"確定") { [deleteDic removeObjectForKey:[dataArray objectAtIndex:indexPath.row]]; }}@end