在编辑模式下,我希望单元格除了编辑附件外还显示一个uibutton。当tableView进入编辑模式时,按钮显示正常并且工作正常。但它永远不会被删除。除了以下代码之外,我还尝试过说myButton.hidden = TRUE。这就是我在自定义tableViewCell类
中的内容- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
self.trashButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.trashButton.frame = CGRectMake(337, 17, 27, 30);
[self.trashButton setBackgroundImage:[UIImage imageNamed:@"trash.png"] forState:UIControlStateNormal];
if (editing) {
[self addSubview:self.trashButton];
} else {
[self.trashButton removeFromSuperview];
}
}
答案 0 :(得分:1)
每次调用setEditing方法时,都在重新创建UIButton。当您调用removeFromSuperview时,正在删除的按钮就是您刚刚创建的几行,并且它不是添加到UITableViewCell的按钮。
解决方案:使UIButton成为一个类属性,在initWithFrame或awakeFromNib中初始化它(如果使用storyboard),然后在调用setEditing时将其隐藏/显示
答案 1 :(得分:1)
正如我在上面的评论中提到的,这样做可能会更好:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
if (self.trashButton)
{
[self.trashButton removeFromSuperview];
self.trashButton = nil;
}
else
{
self.trashButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.trashButton.frame = CGRectMake(337, 17, 27, 30);
[self.trashButton setBackgroundImage:[UIImage imageNamed:@"trash.png"] forState:UIControlStateNormal];
[self addSubview:self.trashButton];
}
}