2015-05-13 54 views
1

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

我知道要做到这一点苹果的标准方式是右侧显示的删除按钮,当我点击它,数据源的

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

介入,原因我不想做这样的我的手机是通过滚动视图,其水平滚动,从而滚动显示删除按钮将使它成为烂摊子定制的,所以我不会实现

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
在我的数据源

有什么想法吗?

回答

1

一种方式ü做的是,自定义单元格,并把自己的比如删除单元格的方式,

CustomCellTableViewCell.h定义委托其子类UITableviewCell名称类似CustomCellTableViewCell 创建新的自定义单元格方法例如,

#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 

,并在控制器.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]; 
} 

尝试在新项目U将得到它

+1

好了,这是实现的一个方法,谢谢! – John

0

可以achive使用UIViewController

添加tableview这件事和tableviewcellUIViewController

我使用swift实现了同样的效果。它会给你的想法如何在Objective-C

做下面是代码:

var data:[String] = ["One","Three","Four","Five","Six"] 
@IBOutlet var tableView: UITableView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.navigationItem.rightBarButtonItem = self.editButtonItem() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell 

    cell.textLabel.text = self.data[indexPath.row] 

    if editing 
    { 
     cell.imageView.image = UIImage(named: "Button-Delete-icon.png") 
    } 
    else 
    { 
     cell.imageView.image = UIImage(named: "") 
    } 
    return cell 
} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    if editing 
    { 
     self.data.removeAtIndex(indexPath.row) 
     self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade) 
    } 
} 

override func setEditing(editing: Bool, animated: Bool) { 
    super.setEditing(editing, animated: animated) 
     self.tableView.reloadData() 
} 

希望这将有助于