【问题标题】:Swipe To Delete TableView Row滑动删除 TableView 行
【发布时间】:2012-03-17 07:36:22
【问题描述】:

我有我的数组:

self.colorNames = [[NSArray alloc] 
initWithObjects:@"Red", @"Green",
@"Blue", @"Indigo", @"Violet", nil];

我已经尝试了所有能找到的方法,但总是有错误。我希望能够滑动以显示删除按钮,然后按删除按钮并删除该行。

完整代码(与表格相关的所有内容):

// HEADER FILE
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

  IBOutlet UITableView *tableView;
    NSMutableArray *colorNames;

}
@property (strong, nonatomic) NSArray *colorNames;


@end

// IMPLEMENTATION

#import "ViewController.h"

@implementation ViewController

@synthesize colorNames; 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.colorNames = [[NSMutableArray alloc] 
    initWithObjects:@"Red", @"Green",
    @"Blue", @"Indigo", @"Violet", nil];

    [super viewDidLoad];
    //[tableView setEditing:YES animated:NO];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
 }

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView  numberOfRowsInSection:(NSInteger)section {
    int count = [colorNames count];
    return count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}  

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

 }
     // Configure the cell.
     cell.textLabel.text = [self.colorNames objectAtIndex: [indexPath row]];

 return cell;

 }

【问题讨论】:

  • 您只在此处发布了NSArray。请发布您如何设置UITableView的相关代码。
  • 它就在那里。请提供您可能有的任何可能的意见,因为我感谢任何形式的帮助。

标签: ios objective-c uitableview cocoa-touch


【解决方案1】:

实现以下方法:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

【讨论】:

  • 我已经添加了。我需要做的实际上是允许删除。
  • 如果你只是想删除,你似乎不需要实现这个方法,因为“删除”按钮是默认行为。
【解决方案2】:

您必须实现必要的UITableViewDelegate 和UITableViewDataSource 方法。

首先,添加:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

那么:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //remove the deleted object from your data source.
        //If your data source is an NSMutableArray, do this
        [self.dataArray removeObjectAtIndex:indexPath.row];
        [tableView reloadData]; // tell table to refresh now
    }
}

【讨论】:

  • 我已经做到了,但是我需要知道在这些方法中实际放入什么。我已经做到了十多次,但由于教程过时,我总是遇到某种障碍。
  • 这些方法自 iOS 诞生以来并没有太大变化。过时的教程应该可以正常工作。
  • 似乎这是正确的方法。我会检查一下。感谢您的帮助 =)
  • 仅当您愿意提供更多帮助时,当我按下删除时,我会收到 SIGABRT 记录以下内容:“[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x6a0fc20”
  • 另外添加 [tableView reloadData];就在方法的最后一个括号之前。
【解决方案3】:

首先,您的colorNames 应该是 NSMutableArray 而不是 NSArray。您不能在常规(非可变)数组中添加或删除对象;每次进行更改时都必须重新创建它。切换它将使这更容易。对于-tableView:commitEditingStyle:forRowAtIndexPath: 的实现,您将能够执行以下操作:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        [colorNames removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

【讨论】:

  • 可变数组部分有帮助,不知道为什么之前没抓到!
  • +1 用于 [tableView deleteRowsAtIndexPaths] 以进行 UI 更新以及数据源
【解决方案4】:

这对我有用

-(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    if (editingStyle == UITableViewCellEditingStyleDelete){
        Comment *comment = [self.commentArray objectAtIndex:indexPath.row];
        dispatch_async(kBgQueue, ^{
            if([self.thePost deleteCommentForCommentId:comment.commentId]){
                if (indexPath.row < self.commentArray.count) {
                    [self.commentArray removeObjectAtIndex:indexPath.row];
                }
                dispatch_async(dispatch_get_main_queue(), ^{

                    [self.tvComments reloadData];
                });
            }
        });
    }
     [self.tvComments reloadData];
}

【讨论】:

    【解决方案5】:

    Swift 版本:

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
         if editingStyle == UITableViewCellEditingStyle.Delete {
             dataArray?.removeAtIndex(indexPath.row)
             tableview.reloadData()
         }
    }
    

    【讨论】:

      【解决方案6】:

      Swift 3 和 Swift 4 回答不使用 reloadData

      我更喜欢使用 deleteRows 而不是使用 reloadData。

      func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
          // Enables editing only for the selected table view, if you have multiple table views
          return tableView == yourTableView
      }
      
      func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
          if editingStyle == .delete {
              // Deleting new item in the table
              yourTableView.beginUpdates()
              yourDataArray.remove(at: indexPath.row)
              yourTableView.deleteRows(at: [indexPath], with: .automatic)
              yourTableView.endUpdates()
          }
      }
      

      【讨论】:

      • deleteRows 已经为您处理了 tableView 更新。您可以省略开始/结束更新。
      【解决方案7】:

      Swift 4 版本:

      func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
          let delete = UITableViewRowAction(style: .destructive, title: "delete") { (action, indexPath) in
              // delete item at indexPath
      
          }
          return [delete]
      }
      

      【讨论】:

        【解决方案8】:

        您必须添加 UITableViewDelegate 和 UITableViewDataSource 方法。

        您可以在 UITableView Row Swipe 上添加多个按钮:

        - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
                return YES;
            }
        
        -(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
            
                UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                {
                    // Delete something here
                }];
                delete.backgroundColor = [UIColor redColor];
            
                UITableViewRowAction *more = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" More " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                {
                   //You function call:
                }];
                more.backgroundColor = [UIColor colorWithRed:0.188 green:0.514 blue:0.984 alpha:1];
            
                return @[delete, more]; //array with all the buttons.
            }`
        

        【讨论】:

          猜你喜欢
          • 2021-05-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-08
          • 2016-08-09
          • 1970-01-01
          相关资源
          最近更新 更多