iOS删除tableview行
我的应用程序在iOS 6上运行良好。升级后,我无法从我的iOS删除tableview行,ios,row,tableview,Ios,Row,Tableview,我的应用程序在iOS 6上运行良好。升级后,我无法从我的UITableView中删除行 我的原型电池里有一个按钮 按钮的功能如下: - (IBAction)deleteCell:(id)sender { UIButton *btn = (UIButton*) sender; UITableViewCell *cell = (UITableViewCell*) btn.superview.superview; NSIndexPath *indexPath = [_table
UITableView
中删除行
我的原型电池里有一个按钮
按钮的功能如下:
- (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];
}
}
- (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];
}
}
问题是我的索引变量总是0
我尝试了另一种解决方案:
- (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];
}
}
- (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];
}
}
这个解决方案也不起作用。向右滑动时不会发生任何情况。要在单元格滑动时显示删除按钮,必须实现此委托方法
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
试试这个,我希望这对你有用。你可以在
cellforrowatinexpath
中设置按钮tag=cell.index
然后
UITableViewCell *cell = (UITableViewCell *)
[tableView cellForRowAtIndexPath:
[NSIndexPath indexPathForRow:btn.tag inSection:0]];
因此,您可以获取索引试试这个示例教程 ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *arryData1;
NSMutableArray *arryData2;
IBOutlet UITableView *tableList;
}
@end
我想问题是您没有再次重新加载数据,因此数据将保留在缓存中 您可以尝试使用
[tableView reloadData]重新加载数据代码>将此方法添加到下面的
[\u tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]with RowAnimation:UITableViewRowAnimationRight];
第二个解决方案中的此代码行。使用tableview CommittedItingStyle方法删除tableview控制器的单元格/行
以下是可行的代码片段:
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[ArrayHoldsCellObj removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
请注意,ArrayHoldsCellObj
是必须声明的数组对象,并将每个单元格分配给区域索引。dataSourceArray是单元格内容所在的数组comes@ravindran.. 如果假设您在按下delete按钮后得到用户的确认,然后删除行是用户按下Ok按钮。你会怎么做?
- (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];
}
}