UITableViewCell上的动画按钮,不会影响其他UITableViewCell

时间:2014-10-20 04:38:27

标签: ios objective-c iphone uitableview

我有一个带有以下代码的UITableViewCell,用户点击" Save" 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];

}];

3 个答案:

答案 0 :(得分:3)

您正在通过实施打破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

答案 1 :(得分:0)

您需要以编程方式为每个单元格创建一个新按钮。

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
        action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];`

您可以将标签添加为某种类型的标识符。

[button setTag:rowNumber];

答案 2 :(得分:0)

试试这个..

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

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

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

           UIButton * cellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
          [cellButton setTitle:@"Save" forState:UIControlStateNormal];
          [cellButton addTarget:self action:@selector(saveImage:) forControlEvents:UIControlEventTouchUpInside];
          cellButton.frame = CGRectMake(127, 8, 50, 30);
          [cell.contentView addSubview:cellButton];

        return self.tableCell;

    }

    -(void)saveImage:(UIButton*)sender{

    //image is saved

    [UIView animateWithDuration:0.5f animations:^{

        }completion:^(BOOL finished) {

                [sender removeFromSuperview];


    }];