ios - 通过点击左侧的删除控件直接删除 UITableViewCell,但不显示右侧的删除按钮

标签 ios uitableview uiscrollview edit

当我涉及[tableView setEditing:YES animated:YES]时,删除控件显示在左侧的每个单元格上,我想要做的是在我点击删除控件时获取事件,并且直接删除单元格但不在右侧显示删除按钮。

我知道苹果的标准做法是在右侧显示删除按钮,当我点击它时,数据源的

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

参与其中,我不想这样做的原因是我的单元格是由水平滚动的 ScrollView 自定义的,因此滚动以显示删除按钮会使它变得一团糟,所以我不会实现

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

在我的数据源中。

有什么想法吗?

最佳答案

你做的一种方法是,自定义单元格并以你自己的方式删除单元格,例如,

通过子类化 UITableviewCell 创建一个新的自定义单元格,将其命名为类似 CustomCellTableViewCellCustomCellTableViewCell.h 中定义一个委托(delegate)方法,例如,

 #import <UIKit/UIKit.h>

 @class CustomCellTableViewCell;
 @protocol CellDelegate <NSObject>
 - (void)deleteCell:(CustomCellTableViewCell *)cell;
 @end


 @interface CustomCellTableViewCell : UITableViewCell
 + (CustomCellTableViewCell *)createCell;
 @property (weak, nonatomic) IBOutlet UIButton *deleteButton;
 @property (weak, nonatomic) IBOutlet UILabel *descriptionLabel;
 @property (weak,nonatomic)  id<CellDelegate> cellDelegate;
 - (IBAction)deleteAction:(id)sender;
 - (void)showDeleteButton;
 - (void)hideDeleteButton;
 @end

并在 CustomCellTableViewCell.xib 中添加一个按钮并设置标签连接到 deleteButtondescriptionLabel

CustomCellTableViewCell.m 文件中

 #import "CustomCellTableViewCell.h"

 @implementation CustomCellTableViewCell

 - (void)awakeFromNib {
  // Initialization code
   }


 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
      self = [CustomCellTableViewCell createCell];
    }
    return self;
 }

 + (CustomCellTableViewCell *)createCell
 {
     NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CustomCellTableViewCell" owner:nil options:nil];
     if ([arrayOfViews count] < 1) {
        return nil;
     }
     for (id item in arrayOfViews) {
       if([item isKindOfClass:[UITableViewCell class]])
           return item;
     }
     return nil;
 }

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  [super setSelected:selected animated:animated];

   // Configure the view for the selected state
 }

 - (IBAction)deleteAction:(id)sender {
    if([self.cellDelegate respondsToSelector:@selector(deleteCell:)])
    {

       [self.cellDelegate deleteCell:self];
    }
  }

 - (void)showDeleteButton
 {
   CGRect destRect = self.descriptionLabel.frame;
   destRect.origin.x += 80;
   [UIView animateWithDuration:0.3 animations:^{
    self.descriptionLabel.frame = destRect;
   }];
 }

 - (void)hideDeleteButton
 {
   CGRect destRect = self.descriptionLabel.frame;
   destRect.origin.x = 0;
   [UIView animateWithDuration:0.3 animations:^{
     self.descriptionLabel.frame = destRect;
   }] ;

 }
 @end

在 Controller .m文件中

- (void)viewDidLoad {
   [super viewDidLoad];
   stringsArray  = [[NSMutableArray alloc]initWithObjects:@"apple",@"dell",@"windows",@"nokia",@"sony",@"hp",@"lenovo", nil];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return [stringsArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CustomCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SuggestionCell"];
  if(cell == nil)
  {
     cell = [[CustomCellTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SuggestionCell"];
  }

  if(customEditTableView)
     [cell showDeleteButton];
  else
     [cell hideDeleteButton];

  cell.cellDelegate = self;
  cell.descriptionLabel.text = [stringsArray objectAtIndex:indexPath.row];
  return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return 50.0f;
}
- (IBAction)deleteCellsAction:(id)sender
{
  if(customEditTableView)
      customEditTableView = NO;
  else
      customEditTableView = YES;

  [self.aTableView reloadData];

}

- (void)deleteCell:(CustomCellTableViewCell *)cell
{
   NSIndexPath *indexPath = [self.aTableView indexPathForCell:cell];
   [stringsArray removeObjectAtIndex:indexPath.row];
   [self.aTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

在新项目中尝试你会得到它

关于ios - 通过点击左侧的删除控件直接删除 UITableViewCell,但不显示右侧的删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30204558/

相关文章:

ios - cellForRowAtIndexPath 和 prepareForSegue 返回不同的标签颜色

ios - UIWebView被键盘隐藏

ios - UIScrollView iOS 应用中的动画

ios - 如何在 ScrollView Swift 中为图像变化设置动画

ios - 合并 iOS 应用程序

ios - 在旋转时水平展开多个 UITextField(通过自动布局)

ios - 在 TextView 中查找子字符串的 CGPoint 位置

ios - swift 4 : New Viewcontroller when tapped on a cell with separate class

ios - iBeacon - 当应用程序在区域中启动时没有调用 didEnterRegion

iOS:使用 LaunchImage 作为应用背景