在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
新聞熱點
疑難解答