【问题标题】:setting tag value for button to unhide on a cell click设置按钮的标记值以在单元格单击时取消隐藏
【发布时间】: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


【解决方案1】:

改变这个 ===>

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];
    [cell addSubview:record1];

}

到 ==>

  if(tableView == self.todayTableView)
    {
 for (__strong UIView *view in Cell.contentView.subviews) {
       if(view.tag==101){
        [view removeFromSuperview];
        view = nil;}
    }
      record1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
record1.tag=101;
            [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.contentView addSubview:record1];

}

/********替换didSelect********************/

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView == _todayTableView)
    {
        UITableViewCell *cell = [tableView 
     cellForRowAtIndexPath: indexPath]; 

      for (__strong UIView *view in Cell.contentView.subviews) {
           if(view.tag==101){
           view.alpha=1;
           break;
        }


    }
}

【讨论】:

  • 在“NSIndexPath”上找不到属性 lastIndexPath
  • 抱歉,换成 indexPath
  • 请告知问题
【解决方案2】:

我认为您应该重新加载 tableview 或重新加载单元格。你可以试试这段代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    // change Alpha button
    if(tableView == _todayTableView) {
        [cell.record1 setAlpha:0];
    }
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

【讨论】:

    【解决方案3】:

    添加按钮无需使用for循环,每次添加新行时都会调用cellForRowAtIndexPath:方法。

    请尝试以下代码:

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

    if(tableView == self.todayTableView)
    {
            UIButton *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)];
    
        [record1 setTag:indexPath.row];
    
    }
        return cell;
    

    }

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    YourCellClass *cell = (YourCellClass*)[tableView cellForRowAtIndexPath:indexPath];
    UIButton *record1 = (UIButton*)[cell viewWithTag:8];
    
    if(tableView == _todayTableView)
    {
        if(indexPath.row == 0)
        {
            if( record1 != nil)
            {
                [record1 setAlpha:1];
    
            }
    
        }
    }
    

    }

    【讨论】:

    • 感谢 for 循环的建议 :) 它显示在每个单元格上,但是当我不使用 for 循环时,它显示的图像有些模糊。
    • 我没有为此使用自定义单元格。
    • 更改按钮的图标图像,使用分辨率更高的图像。如果你不使用自定义单元格,那么你可以简单地使用 UITableViewCell 类来创建你的单元格对象。
    【解决方案4】:

    您似乎将record1 Button 存储在您班级的@property 变量中。不要这样做,因为对于将创建的每个单元格,新按钮将一遍又一遍地保存到相同的@property。所以最后,实际上只有最后一个创建的 UIButton 可供您使用,因为所有之前创建的 UIButton 都被覆盖了。

    以下是您的问题的解决方案:

    1.) 在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {} 函数中,您应该为record1-Button 创建一个局部变量,如下所示:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if(tableView == self.todayTableView) {
            for (int i = 0; i < first.count; i++) {
                // create NEW button, don't store it in a @property reference
                UIButton* record1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
                // now continue as you did
    }
    

    2.) 在你的 UITableViewCell 中只有一个 UI-Element 获取 indexPath.row 属性的标签是很重要的。因为那时您可以通过其标签属性访问 UI 元素(在您的情况下为按钮):

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        // get the UITableViewCell
        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    
        // get the button and set its alpha property
        UIButton* record1 = (UIButton*) [cell viewWithTag:indexPath.row];
        record1.alpha = 1.0;
    }
    

    还有一个提示:第一个单元格将 indexPath.row 作为“0”值。这也可能会导致问题,因为默认情况下,单元格中的每个 UIView-Element 都会有一个 tag=0 。因此,例如分配 indexPath.row + 100 之类的标签可能会更好。

    【讨论】:

    • 感谢您的回答!!!正如您所说,需要为按钮创建局部变量,我最初是这样做的,但后来我无法在 CellForRowAtIndexPath 之外访问它......所以我将它全局声明为 UIButton *record1;
    【解决方案5】:

    尝试一次

    1.Create a mutable Array globally as 
    @interface YourClassName ()
    {
        NSMutableArray*selectedCellArr;
    }
    
    2.In ViewDidLoad Allocate Memory to that array as selectedCellArr = [NSMutableArray new];
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
    {
    
        if(tableView == self.todayTableView)
        {
    
           UIButton*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)];
           [record1 setTag:indexPath.row];
           [cell addSubview:record1];
    
        if ([selectedCellArr containsObject:indexPath]) [record1 setAlpha:1];
    
           return cell;
        }
    }
    
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if(tableView == _todayTableView)
        {
           if ([selectedCellArr containsObject:indexPath]) {
                 [selectedCellArr removeObject: indexPath];
           }else{
                 [selectedCellArr addObject: indexPath];
           }
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      • 2014-08-02
      • 2018-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多