【问题标题】:Enabling UIButton in TableViewCell enables in more Cells在 TableViewCell 中启用 UIButton 可以在更多单元格中启用
【发布时间】:2016-03-29 15:10:33
【问题描述】:

我有一个TableViewController 和我自定义的TableViewCell,每行有两个按钮,一个隐藏另一个不隐藏,当我点击显示的按钮时,我想显示另一个按钮,我已经介绍过了这个,我的问题是,如果我向下滚动,其他隐藏按钮会因为重用标识符而显示。

我该怎么做才能只显示我选择的行中的按钮。

希望我清楚,如果没有请问我。

谢谢大家。

【问题讨论】:

  • 禁用单元格的prepareForReuse 方法中的按钮。

标签: ios objective-c uitableview uibutton


【解决方案1】:

当您滚动 tableView 时,其余按钮被隐藏,因为您在 cellForRowAtIndexPath 中编写了 setHidden 方法,没有设置任何条件,因此取一个 NSMutableArray 并为其分配内存。无论您选择什么索引,只需添加它 MutableArray。然后在 cellForRowAtIndexPath 中放置一个条件,如果该数组包含 indexPath,则不要隐藏,否则隐藏按钮。

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  CutomTableView *cell=[tableView dequeueReusableCellWithIdentifier:@"CellId" forIndexPath:indexPath];
    [cell.show setTag:indexPath.row];
    [cell.show addTarget:self action:@selector(showButtonACtion:) forControlEvents:UIControlEventTouchUpInside];
NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
//self.selectedRow is NSMutable Array
   if ([self.selectedRow containsObject:selectedIndexPath]) {
        [cell.hideBtn setHidden:NO];
    }else{
        [cell.hideBtn setHidden:YES];
    }
    return cell;
}
-(void)showButtonACtion:(UIButton*)sender{
   NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
    [self.selectedRow addObject:myIP];
     [self.tableV reloadData];
    }

【讨论】:

  • 谢谢 Rubiya 我会尝试一下。我会让你如何结束。
【解决方案2】:

当您单击特定按钮以显示另一个按钮时,只需保存该 NSIndexpath。

例如, 如果您单击第二行,则在 NSMutableDicionary 中添加值

[mutableDict setValue:@"YES" forKey:[NSString stringWithFormat:@"%@", indexPath.row]];

然后在 cellForRowAtIndexPath 方法中检查特定的 indexPath。

每当您为特定行找到 @"YES" 时,实现您想要的逻辑来显示或隐藏按钮。

希望对你有所帮助。

【讨论】:

  • 谢谢 Dhruvik,但是按钮是在 TableViewCell.m 中处理的,我的意思是在此处捕获的操作,我如何将 TableViewCell 中填写的 NSMutableDictionary 信息发送到 TableViewController 以便在 cellForRowAtIndexPath 方法中进行处理告诉我了吗?
  • 为此,您可以实现委托来实现此目的。 @TonyM。
【解决方案3】:
-(IBAction)firstButton:(UIControl *)sender
{
        UIButton *button = (UIButton*)sender;
        NSIndexPath *myIP = [NSIndexPath indexPathForRow:sender.tag inSection:0];

        CustomCell *cell = (CustomCell*)[self.tableView cellForRowAtIndexPath:myIP];
       [cell.button2 setHidden:NO];.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 2012-04-17
    相关资源
    最近更新 更多