我的代码中有一个简单的关键问题。
在我的 Tableview 上,当我按下我的编辑按钮(在导航栏上)时,我需要编辑 UITableview 的方法。我想隐藏 My Cell 上的按钮和标签。
代码:
我正在像这样添加我的 Button ..
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *currentCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
currentCell = nil;
if (currentCell==nil)
{
currentCell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
BtnEmail=[UIButton buttonWithType:UIButtonTypeCustom];
//BtnEmail = (UIButton *)[currentCell viewWithTag: 3];
BtnEmail.frame=CGRectMake(265, 17, 25, 25);
BtnEmail.tag=indexPath.row;
//[BtnEmail setTitle:@"E-mail" forState:UIControlStateNormal];
[BtnEmail setBackgroundImage:[UIImage imageNamed:@"email.png"] forState:UIControlStateNormal];
[BtnEmail addTarget:self action:@selector(BtnEmail:) forControlEvents:UIControlEventTouchUpInside];
[currentCell.contentView addSubview:BtnEmail];
[currentCell.contentView bringSubviewToFront:BtnEmail];
return currentCell;
}
我的编辑按钮是这样声明的
编辑按钮:
self.navigationItem.rightBarButtonItem = self.editButtonItem;
然后在编辑时单击我的此方法将调用。
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[listTableView setEditing:editing animated:animated];
if(editing)
{
self.navigationItem.leftBarButtonItem.enabled = NO;
//BtnEmail.frame=CGRectMake(355, 17, 25, 25);
BtnEmail.hidden = TRUE;
}
else
{
self.navigationItem.leftBarButtonItem.enabled = YES;
//BtnEmail.frame=CGRectMake(265, 17, 25, 25);
BtnEmail.hidden = FALSE;
}
[super setEditing:editing animated:animated];
}
在这种情况下,我的最后一个单元格 Button 和 Lable 不会全部隐藏。我需要隐藏我的 Cell 的所有 UIButton 。
如果 我在 table 上有 3 个单元格,那么它只会隐藏最后一个按钮 ..
谢谢。
最佳答案
您可以简单地在 cellForRow
中进行条件检查并在编辑更改时重新加载 TableView 。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Do your other stuff
// Add this conditional check
if (tableView.editing)
{
BtnEmail.hidden = YES;
}
else
{
BtnEmail.hidden = NO;
}
[currentCell.contentView addSubview:BtnEmail];
[currentCell.contentView bringSubviewToFront:BtnEmail];
return currentCell;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[listTableView setEditing:editing animated:animated];
// Reload the table view
[listTableView reloadData];
// Do your other stuff
}
但是使用自定义单元格很容易创建该按钮。在这里,您一次又一次地添加按钮。否则将创建代码的按钮移动到 if(cell == nil)
block
关于iphone - 隐藏 UITableviewcell 的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15087434/