【发布时间】:2014-10-20 04:38:27
【问题描述】:
我有一个带有以下代码的 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];
}];
【问题讨论】:
-
带有标识符方法的cell alloc or deque cell在哪里
-
在 cellForRow 中。为简洁起见,最初省略。在上面添加。
-
为每个单元格创建按钮并添加标签并进行相应检查。
-
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [按钮 addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];这需要来那个地方而不是你的 [self.tableCell.saveImageButton addTarget:self action:@selector(saveImage:) forControlEvents:UIControlEventTouchUpInside];或者去自定义单元格并使用委托来调用按钮
-
但这之后会发生什么?我知道这一行,但因为现在它不是我不能做的属性 [cell.view removeFromSuperview];它有动画效果,但永远不会被移除。
标签: ios objective-c iphone uitableview