Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS删除tableview行_Ios_Row_Tableview - Fatal编程技术网

iOS删除tableview行

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

我的应用程序在iOS 6上运行良好。升级后,我无法从我的
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];

}    
}