【问题标题】:iOS delete a tableview rowiOS 删除一个 tableview 行
【发布时间】:2013-09-28 11:41:11
【问题描述】:

我的应用在 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];
    } 
} 

问题是我的索引变量总是0。

我尝试了另一种解决方案:

- (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];

}
}

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

【问题讨论】:

    标签: ios row tableview


    【解决方案1】:

    试试这个示例教程,

    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;
    }
    

    【讨论】:

    • @ravindran.. 如果假设您在按下删除按钮后得到用户的确认,然后删除行是用户按下确定按钮。你会怎么做?
    【解决方案2】:

    我怀疑问题是您没有再次重新加载数据,因此它停留在高速缓存中

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

    【讨论】:

      【解决方案3】:

      要在单元格滑动时显示删除按钮,您必须实现此委托方法。

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

      试试这个,我希望这对你有用。

      【讨论】:

        【解决方案4】:

        使用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 对象。

        【讨论】:

          【解决方案5】:

          您可以在cellForRowAtIndexPath中设置按钮tag = cell.index

          然后

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

          所以你可以得到索引

          【讨论】:

            【解决方案6】:
            - (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];
            
            }    
            }
            

            【讨论】:

            • dataSourceArray 是单元格内容来自的数组
            猜你喜欢
            • 2016-12-25
            • 1970-01-01
            • 2013-01-02
            • 1970-01-01
            • 2017-03-17
            • 1970-01-01
            • 2013-12-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多