ios - 在不影响其他 UITableViewCell 的情况下为 UITableViewCell 设置动画按钮

标签 ios objective-c iphone uitableview

我有一个带有以下代码的 UITableViewCell,其中用户单击 UITableViewCell 上的“保存”按钮。单元格中的图像已保存,“保存”按钮动画消失,但单元格仍然存在。

   -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  if (!self.tableCell)
    {
        [tableView registerNib:[UINib nibWithNibName:@"LPContentTableViewCell" bundle:nil] forCellReuseIdentifier:@"ContentCell"];

        self.tableCell = [tableView dequeueReusableCellWithIdentifier:@"ContentCell"];
    }

        [self.tableCell.saveImageButton addTarget:self action:@selector(saveImage:) forControlEvents:UIControlEventTouchUpInside];

        return self.tableCell;

    }

    -(void)saveImage:(id)sender{

     UIButton *button = (UIButton*)sender;

    //image is saved

    [UIView animateWithDuration:0.5f animations:^{
            button.frame = newFrame;
        }completion:^(BOOL finished) {

                [button removeFromSuperview];


    }];

问题是,在我按下保存时未分配的单元格也丢失了它们的保存按钮。我将按钮转换为发件人。按钮被移除,接下来几个单元格上的按钮保留。但是,当我在没有按钮的情况下滚动时,会出现 UITableView 中更靠下的单元格。想法?

编辑:合并建议

UIButton * cellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cellButton setTitle:@"Save" forState:UIControlStateNormal];
[cellButton addTarget:self action:@selector(saveImage:) forControlEvents:UIControlEventTouchUpInside];
cellButton.frame = CGRectMake(268, 95, 79, 23);
[self.tableCell.contentView addSubview:cellButton];


-(void)saveImage:(id)sender{
UIButton *button = (UIButton*)sender;


CGRect newFrame = button.frame;

newFrame.origin.x +=100;

[UIView animateWithDuration:0.5f animations:^{
   button.frame = newFrame;

}completion:^(BOOL finished) {

        [button removeFromSuperview];

}];

最佳答案

您的实现打破了 MVC 模式。

cellForIndexPath 会在单元格即将显示在屏幕上时被调用,并且应该避免需要记住有关您的模型(图像)的任何信息。使用 indexPaths 在方法中访问您的模型。

实现它的正确方法是将其分开。

-(void) viewDidLoad
{
    [super viewDidLoad];

    // Register you nib here. You only need to do this once.
    [self.tableView registerNib:[UINib nibWithNibName:@"LPContentTableViewCell" bundle:nil] forCellReuseIdentifier:@"ContentCell"];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Dequeuing will create or reuse cell. This is what that iOS silky smooth scrollin is all about
    LPContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContentCell" forIndexPath: indexPath];

    BOOL canSave = YES; //figure out if image at indexPath can be saved..

    NSArray *targets = [cell.saveImageButton actionsForTarget:self forControlEvent:UIControlEventTouchUpInside];

    if ([targets count] == 0) {
        // Only add the target once per cell.. it would be easier to just add the target in IB instead of this method.
        [cell.saveImageButton addTarget:self action:@selector(saveImage:) forControlEvents:UIControlEventTouchUpInside];
    }


    if (canSave) {
        cell.saveImageButton.alpha = 1;
    }
    else {
        cell.saveImageButton.alpha = 0;
    }
}

-(void)saveImage:(id)sender {
    UIButton *button = (UIButton*)sender;
    LPContentTableViewCell *cell = [button superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    // Get image using indexPath and Save

    CGRect newFrame = button.frame;
    newFrame.origin.x +=100;

    [UIView animateWithDuration:0.5f animations:^{
       button.frame = newFrame;

    }completion:^(BOOL finished) {

        // Use alpha instead of removing the view so reused cells can just set the alpha back to 1
        button.alpha = 0;

        // Set the old frame back here too for reused cells.
        button.frame = oldFrame;
    }];
}

LPContentTableViewCell 的界面应该看起来像这样。

@interface LPContentTableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIButton *saveImageButton;  // This needs to be connected with IB

@end

关于ios - 在不影响其他 UITableViewCell 的情况下为 UITableViewCell 设置动画按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26458663/

相关文章:

objective-c - 尝试更改 UITextView.text 时应用程序崩溃

iOS UI 没有显示它应该在的位置

iphone - iphone 无法解释的段错误

iphone - 16 秒在 iPhone 上的 SQLite 数据库中插入 1000 行?

iphone - UIImageView 动画在 UITableView 中停止

ios - 核心数据 : Programmatically inherited relations become nil

ios - 如何在 Xcode 11 中制作连接到 Firebase 的搜索栏页面

ios - 如何将值从 viewControllerA 发送到 viewControllerB?

objective-c - Obj-C、NSTimer 循环调度,正确的模式?

ios - Xcode 自动完成以在 "Contains"而不是“开始于”时显示建议