iOS删除一个tableview行

我有我的应用程序在iOS 6上正常工作。随着升级,我停止能够从我的UITableView删除行。

我在原型单元中有一个button。

这是button的function:

 - (IBAction)deleteCell:(id)sender { UIButton *btn = (UIButton*) sender; UITableViewCell *cell = (UITableViewCell*) btn.superview.superview; NSIndexPath *indexPath = [_tableView indexPathForCell:cell]; if([names count] > 9) { int index = indexPath.row; [names removeObjectAtIndex:index]; [photos removeObjectAtIndex:index]; [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; } } 

问题是我的索引variables始终为0。

我试了另一个解决scheme

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { int index = indexPath.row; [names removeObjectAtIndex:index]; [photos removeObjectAtIndex:index]; [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; } } 

此解决scheme也不起作用。 当我向右滑动时没有任何反应。

试试这个示例教程,

ViewController.h

 #import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> { NSMutableArray *arryData1; NSMutableArray *arryData2; IBOutlet UITableView *tableList; } @end 

ViewController.m

 #import "ViewController.h" @interface ViewController () @end @implementation ViewController -(void)viewDidLoad { [super viewDidLoad]; arryData1 = [[NSMutableArray alloc] initWithObjects:@"MCA",@"MBA",@"BTech",@"MTech",nil]; arryData2 = [[NSMutableArray alloc] initWithObjects:@"Objective C",@"C++",@"C#",@".net",nil]; tableList.editing=YES; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [arryData1 count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier= @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ; } cell.textLabel.text = [arryData1 objectAtIndex:indexPath.row]; cell.detailTextLabel.text = [arryData2 objectAtIndex:indexPath.row]; return cell; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { int index = indexPath.row; [arryData1 removeObjectAtIndex:index]; [arryData2 removeObjectAtIndex:index]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } } - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableList.editing) { return UITableViewCellEditingStyleDelete; } return UITableViewCellEditingStyleNone; } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } 

我想问题是你没有重新加载数据,因此它停留在高速缓冲存储器中

你可以尝试用[tableView reloadData];重新加载数据[tableView reloadData]; 这个方法下面的[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; 这个代码行在你的第二个解决scheme。

要在单元格上显示删除button,您必须实现此委托方法。

 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return YES if you want the specified item to be editable. return YES; } 

试试这个,我希望这会为你工作。

使用tableview commiteditingstyle方法删除tableviewcontroller的一个单元格/行。

这是可行的代码片段:

 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { [ArrayHoldsCellObj removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } 

请注意, ArrayHoldsCellObj是必须声明的Array对象,并将每个单元格分配给区域索引。

您可以在cellForRowAtIndexPath设置button tag = cell.index

接着

 UITableViewCell *cell = (UITableViewCell *) [tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:btn.tag inSection:0]]; 

所以你可以得到索引

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //add code here for when you hit delete [dataSourceArray removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } }