【发布时间】:2017-05-18 10:07:23
【问题描述】:
我在 tableview 单元格上有按钮,它是通过编程添加的,我使用 for 循环在每个单元格上添加了这个按钮,我也将它设置为“alpha value = 0”。我想要的是,当我单击特定单元格时,它应该只显示该单元格上的按钮。
我做了什么:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
if(tableView == self.todayTableView)
{
for (int i = 0; i < first.count; i++)
{
record1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[record1 setAlpha:0];
record1.frame = CGRectMake(250, 23, 25, 25);
[record1 addTarget:self action:@selector(record:) forControlEvents:UIControlEventTouchUpInside];
[record1 setImage:[UIImage imageNamed:@"microphone-g-ico.png"] forState:(UIControlStateNormal)];
[cell addSubview:record1];
}
[record1 setTag:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == _todayTableView)
{
if(indexPath.row == 0)
{
if( record1.tag == 8)
{
[record1 setAlpha:1];
}
}
}
}
当我单击单元格时,它仅在最后一个单元格上显示按钮。
请建议??
【问题讨论】:
-
你是在每个单元格上添加一个按钮还是多个按钮
-
@VinodKumar 每个单元格上的相同按钮。当我单击特定单元格时,我只希望该单元格上的按钮,其他应该隐藏
-
您可以使用模型类,因为当您滚动显示在不同单元格上的表格视图时,单元格会重用@iOSBeginner
-
@VinodKumar 模型类 ??抱歉,也许这是个愚蠢的问题,但我是 iOS 的新手。
-
我会在我的 answer@iOSBeginner 中为您提供完整的代码
标签: ios objective-c uitableview tags