【问题标题】:how to hide/show a specific view inside the custom tableview cell when button click inside the same cell当在同一个单元格内单击按钮时,如何在自定义表格视图单元格内隐藏/显示特定视图
【发布时间】:2016-09-14 06:51:56
【问题描述】:
  • 我有一个table view(例如:20 行)。我使用了custom table view cell 和这个table view

  • 在这个table view cell里面,有几个labels,一个button和一个hidden view (UIView)

  • 我已经为hide/show 写了button action hidden viewcustom table view cell class 内。它工作正常。但它会影响表格视图中的其他rows。这意味着,当我点击第一行中的按钮时,隐藏视图会显示,并且当scroll down 时,它可以在表格视图的其他行中看到。

  • 同时(当hide/show),我想increasedecrease的行高(只点击行/单元格)。出了什么问题。下面是我的代码和一些屏幕截图,以供参考。

注意:单击每个单元格中的展开按钮时,单元格会自行展开/增加。

这就是我在custom table view cell 类中hideshow 的方式。

- (IBAction)hideshow:(id)sender {
    BOOL ishidden = self.insideCollectionView.hidden;
    if(ishidden == true)
    {
        self.insideCollectionView.hidden = false;
    }
    else
    {
        self.insideCollectionView.hidden = true;
    }
}

出了什么问题,希望您对此有所帮助。

高级:如果有一种方法可以在单击每个单元格的展开按钮时同时隐藏/显示和展开(增加行高)单元格。

【问题讨论】:

  • 您为什么不在CollectionViewHeaders 中使用带有expand 选项的单个UICollectionView?不太复杂的代码,对于隐藏/扩展,您可以实现 numberOfCellsInRow 方法以相应地将行 count 设置为 0,甚至对此进行动画处理。

标签: ios objective-c uitableview row-height


【解决方案1】:

我自己找到了适合的解决方案。感谢所有支持我的人。

执行以下操作。

  1. 创建一个NSMutableArray 来保存Which buttonWhich row clicked
  2. 那么当用户点击Custom table view cell中的Button时,检查index pathmutable array中的already,如果里面是already,则romove它,否则添加它。
  3. 然后在cellforrowatindexpath方法中,检查nsmutable array和,检查indexpath.rowexist还是not
  4. 最后,如果是exists,就不要隐藏,否则hide就可以了。

这里是表格视图的实现。 .m文件

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 25;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FirstTableViewCell *cells = [tableView dequeueReusableCellWithIdentifier:@"tvcell" forIndexPath:indexPath];

    NSString *theIndexpath = [NSString stringWithFormat:@"%ld", (long)indexPath.row];

//here check whether it is exists or not.
        if ([chekcme containsObject:theIndexpath])
        {
            cells.insideCollectionView.hidden = false;
        }
        else
        {
            cells.insideCollectionView.hidden = true;
        }



    [cells setColletionData:bbarray];

    [cells.expandMe addTarget:self action:@selector(runs:) forControlEvents:UIControlEventTouchUpInside];

//in here set the tag of the button to indexpath.row
    cells.expandMe.tag = indexPath.row;


    return cells;
}

//this is the action for the button inside the custom tableview cell.
- (IBAction)runs:(UIButton *)sender{

    NSString *myob = [NSString stringWithFormat:@"%li", (long)sender.tag];
    NSLog(@"%@",myob);

    if ([chekcme containsObject:myob]) {
        [chekcme removeObject:myob];
    }
    else
    {
        [chekcme addObject:myob];
    }


    NSLog(@"%@", chekcme);

//keep in mind to reload the table view here.
    [self.maintableView reloadData];
}

注意:checkme 是 NSMutableArray 来保存用户点击的对象。

【讨论】:

    【解决方案2】:

    我需要查看您的 tableview 数据源方法中的代码,但我认为以下可以解决您的问题:

    问题 #1:我假设您正在为您的单元格使用双端队列,这会导致您在滚动时重复使用该单元格。我会建议您维护每个单元格的状态(例如:isExpanded)并在 cellForRowAtIndex 中相应地配置单元格:

    问题 #2:在 heightForRowAtIndex: 中使用相同的“IsExpanded”,并在“IsExpanded”的状态更改时为您的单元格在表格上调用 reloadRowsAtIndexPaths:

    【讨论】:

      【解决方案3】:

      该按钮影响多个rows 是因为您将BOOL 存储在collectionView 中的cell 中。

      cell 被另一个row 重用时,根据之前使用的row 的状态,cell 将是hidden/shown。正如已经建议的那样,您需要为每个rows 存储此state,并在dataSource 准备cell 时相应地设置它。

      【讨论】:

        【解决方案4】:

        您应该最初在 cellForRowAtIndexPath 中设置有关视图的状态。希望对你有帮助。

            - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        // here you have to maintain the status about the current cell. whether it is hidden or not.
            cell.insideCollectionView.hidden = status;
            }
        

        【讨论】:

        • 你怎么称呼它self,因为insideCollectionView在我的custom table view cell里面,
        • 能否请您用代码解释一下,将不胜感激。
        • 我的意思是,在 cellForRowAtIndexPath 我们有单元格的引用对吗?用它你可以控制隐藏。
        【解决方案5】:

        我正在做同样的事情,例如: 在 didSelect 中

          switch selectedIndexPath {
            case nil:
                selectedIndexPath = indexPath
            default:
                if selectedIndexPath! == indexPath {
                    selectedIndexPath = nil
                } else {
                    selectedIndexPath = indexPath
                }
            }
          tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        

        在行高

        if selectedIndexPath != nil {
                if selectedIndexPath == indexPath {
                    return estimatedHeight
                }
            }
            return 44
        

        selectedIndexPath 是 NSIndexPath 的一个属性。 希望这对您有所帮助。

        【讨论】:

        • 是的,如果我想在索引路径处选择行,这可以做到,但我想在单击按钮时这样做,因为我想使用在索引路径处选择行来导航到另一种观点。
        • 是的,你也可以用按钮来执行,通过给它们设置标签。
        【解决方案6】:

        您需要重新加载所有表格而不是单个单元格 你可以使用下面的代码

         tbl_name .beginUpdates(
         tbl_name .endUpdates()
        

        更新

        func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
           if indexPath.row == userselectedrow // save selectecd cell index  if you want to only one cell expand otherwise save selected row in array and compare to here
                {
                     return incrementheight // increment row height here
                }
                else
                {
                  return decrementheight // decrement row height
                }      
        
            }
        
        - (IBAction)hideshow:(UIButton*)sender {
         CGPoint point11=[sender convertPoint:CGPointZero toView:tbl_calender];
           NSIndexPath index_pathGlobal =[tbl_calender indexPathForRowAtPoint:point11]; // save index_pathGlobal
        }
        

        希望对你有所帮助

        【讨论】:

        • 怎么用,我不知道怎么用,在哪里用。
        • 在您的按钮单击操作中增加您想要展开的单元格的行高和其他想要减小的行高
        • 你能用必要的代码解释一下吗?提前谢谢
        • 如何保存选定的行,因为我想为单元格内的按钮单击执行此操作,而不是在索引路径处选择行,因为我想在用户单击时导航到另一个控制器行。
        • 检查更新答案使用按钮单击获取索引路径
        【解决方案7】:

        第 1 步:- 假设您在标签值、按钮和隐藏视图等行中显示数据。在数组中添加一个参数,例如。 isViewNeedToShow,默认填充该值FALSE

        第 2 步:- 之后,在您的按钮操作中,您将 indexPath.row 作为标记值传递到索引路径处的行的单元格中,因此在按钮操作中更改数组值参数 isViewNeedToShow == TRUE,然后重新加载表格视图部分。例如:-

        Item *tempItem              = yourArray[sender.tag];
        tempItem. isViewNeedToShow  = tempItem. isViewNeedToShow ? FALSE : TRUE;
        
        **For particular row update** :- 
        
         [yourTableView beginUpdates];
         [yourTableView reloadRowsAtIndexPaths:@[sender.tag, 0] withRowAnimation:UITableViewRowAnimationNone];
         [yourTableView endUpdates];
        

        第 3 步:- 如果要扩展表格单元格,则必须计算包含视图、标签等项目的行高。

        Or if you are using auto layouts in your project use UI Table View Delegate Methods
        
            -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
                return UITableViewAutomaticDimension;
            }
        
            - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
                return UITableViewAutomaticDimension;
            }
        

        希望您的问题能够得到解决。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-02-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多