Ios 在UITableViewCell中隐藏按钮
我现在有一个表,有8行,每行右边有一个标签,左边有一个按钮。我希望我可以隐藏所有按钮,直到用户按下右上角的“编辑”按钮,然后它们就会出现,允许用户与每个表格单元格交互。我不知道这是否可行,因为它们位于Ios 在UITableViewCell中隐藏按钮,ios,objective-c,uibutton,uitableview,Ios,Objective C,Uibutton,Uitableview,我现在有一个表,有8行,每行右边有一个标签,左边有一个按钮。我希望我可以隐藏所有按钮,直到用户按下右上角的“编辑”按钮,然后它们就会出现,允许用户与每个表格单元格交互。我不知道这是否可行,因为它们位于UITableViewCells中,或者是否有更简单的方法为每个单元格调用一个按钮 更新 好的,我已经在所有隐藏的属性中放置了,似乎没有错误,但应用程序无法识别任何错误。尽管按钮被设置为最初隐藏,但它们仍然未隐藏。这是我的密码 这是我的表格单元格代码: - (UITableViewCell *)ta
UITableViewCell
s中,或者是否有更简单的方法为每个单元格调用一个按钮
更新
好的,我已经在所有隐藏的属性中放置了,似乎没有错误,但应用程序无法识别任何错误。尽管按钮被设置为最初隐藏,但它们仍然未隐藏。这是我的密码
这是我的表格单元格代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"BlockCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = @"Free Block";
UIButton*BlockButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
BlockButton.frame = CGRectMake(225.0f, 5.0f, 75.0f, 35.0f);
[BlockButton setTitle:@"Change" forState:UIControlStateNormal];
[BlockButton addTarget:self action:@selector(Switch:) forControlEvents:UIControlEventTouchUpInside];
Blockbutton.backgroundColor = [UIColor colorWithRed:102/255.f
green:0/255.f
blue:51/255.f
alpha:255/255.f];
Blockbutton.hidden = YES;
[cell addSubview:BlockButton];
return cell;
}
这是我的方法代码:
- (IBAction)Editmode:(UIButton *)sender
{
Blockbutton.hidden = !Blockbutton.hidden;
[self.tableView reloadData];
}
关于可能出现的问题有什么想法或想法吗?取一个定义是否显示删除按钮的BOOL变量,将此BOOL变量用于btname.hidden=boolVar,最初使boolVar=NO,当用户点击编辑切换bool var并重新加载tableview时。这里的技巧是要记住,表的单元格由
cellforrowatinexpath:
确定。通过发送表reloadData:
,可以再次调用该方法
因此,只需保留一个BOOL实例变量/属性。使用该按钮切换该实例变量并调用
reloadData:
。如果在调用cellforrowatinexpath:
时,实例变量为YES,则将按钮的hidden
设置为YES;如果否,则为否。如果尚未创建子类,则需要创建一个UITableViewCell
子类。在该类中,重写setEditing:animated:
,如果新值为YES
,则启用/添加/取消隐藏按钮
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing) {
// add your button
someButton.hidden = NO;
} else {
// remove your button
someButton.hidden = YES;
}
}
这是可选的,但如果已设置动画
为是
,则建议您设置更改动画
注意:这假设您已经连接了“编辑”按钮,以更改
UITableView
的编辑模式。如果没有,请在按钮操作中调用UITableView
上的setEditing:animated:
。这将在每个可见的表格单元格上自动调用setEditing:animated:
。另一个选项是测试cellForRowAtIndexPath方法中是否处于编辑模式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = //(obtain your cell however you like)
UIButton *button = cell.button; //(get button from cell using a property, a tag, etc.)
BOOL isEditing = self.editing //(obtain the state however you like)
button.hidden = !isEditing;
return cell;
}
无论何时进入编辑模式,都要重新加载tableView数据。这将使表格视图再次请求单元格,但在这种情况下,按钮将设置为不隐藏
- (void)enterEditingMode {
self.editing = YES;
[self.tableView reloadData];
}
这是可能的。按钮有一个
隐藏的属性,您可以将其设置为是或否。感谢您的回复,但我似乎遇到了问题。我不小心按了enter键并发表了评论,无论如何,这听起来可能很愚蠢,但在我的UITableViewController中,我在哪里可以找到我的UITableView的名称或指针,我不知道是什么对象在调用该方法,因此在我的代码行中出现了一条“选择器重载数据的未知类方法”错误消息,它只是说它是[self.tableView reloadData]
你知道你可以通过查看UITableViewController文档来发现这一点吗?我很确定它被称为“tableview”,但我想我只是有点头脑放屁,忘记了自己。但是谢谢你的提醒,我下次一定会提到它。太棒了!这对我来说非常有效。我只是想补充一点,整个if语句可以用一行代码替换:someButton.hidden=!编辑