TableView编辑中实现多行删除的2中方法以及注意
在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

浙公网安备 33010602011771号