gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:55:53 26 4
gpt4 key购买 nike

我有一个带有以下代码的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com