【问题标题】:Show and hide UIImageView in tableView cell在 tableView 单元格中显示和隐藏 UIImageView
【发布时间】:2016-12-23 12:01:53
【问题描述】:

UIButton 上单击UITableViewCell(自定义类)显示和隐藏UIImageView 的最佳方式是什么。

实际上我的行高是用UITableViewAutomaticDimension 计算的,我已经设置了所有必要的约束。

问题:最好更改单元格高度还是UIImageView 高度?

如果我更改 UIImageView 的高度限制,直到重新加载单元格,高度不会改变。

有什么建议吗?

【问题讨论】:

  • 如果我们改变了你的 imageView 的高度,那么单元格的高度是否会受到影响?
  • 直到 reloadData() 没有被调用
  • 你能提供你的tableviewcell截图吗?这样我们就可以了解您想要什么?
  • 我想这已经很清楚了
  • @vikasprajapati 她在自定义原型单元中有UIImageViewUIButton。现在她想在点击UIButton 时隐藏UIImageView。行高根据原型单元格中的可见内容而定。

标签: ios swift uitableview uiimageview


【解决方案1】:

让你有UITableViewCell 的自定义类说,CustomTableCell。在CustomTableCell 中,您使用UIImageView 的出口(例如,imageView)。对你的UIButton 采取行动(比如,btnShow)。

实现以下方法;

首先在你的 VC 中获取一个全局数组,arrSelected 说

    public func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat{
          return UITableViewAutomaticDimension
    }

    open func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return UITableViewAutomaticDimension
    }

open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let customCell : CustomTableCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomTableCell

        customCell.imageView.tag = indexPath.row;
        customCell.btnShow.tag = indexPath.row;

     var xPos : CGFloat!
     var yPos : CGFloat!
     var width : CGFloat!
     var height : CGFloat!

    xPos = cell.imageView.frame.origin.x
    width = cell.imageView.frame.size.width
    height = cell.imageView.frame.size.height

    if  arrSelected.contains(indexPath.row) { 
        yPos = 100 // whatever the height of imageview when image is present
    cell.imageView.isHidden = false

    }
    else{
     yPos = cell.btnShow.frame.size.height 
     cell.imageView.isHidden = true
    }

        cell.imageView.frame = CGRect(x: xPos, y: yPos, width: width, height: height)         

        return customCell
    }

    // btnShow action

    @IBAction func actionbtnShow(_ sender: UIButton) {

    if sender.isSelected {
         let itemToRemoveIndex = arrSelected.indexOf(sender.tag) {
    arrSelected.removeAtIndex(sender.tag)
        sender.isSelected = !sender.isSelected

    }
    else{
         arrSelected.append(sender.Tag)
        sender.isSelected = !sender.isSelected
    }

    // reload tableView
   }

【讨论】:

    【解决方案2】:

    您可以在动画块中设置隐藏标签,并将 tableView 的 beginUpdates 和 endUpdates 打包在同一块中。

    UIView.animate(withDuration: 0.2, animations:{
        imageView.isHidden = false
        tableView.beginUpdates()
        tableView.endUpdates()
    })
    

    或者在 Objective-C 中:

    [UIView animateWithDuration:0.2 animations:^{
        imageView.hidden = NO;
        [tableView beginUpdates];
        [tableView endUpdates];
    }];
    

    像这样,单元格的高度应该是动画的。

    【讨论】:

    • 这不是 Swift
    • 这不是她问题的答案,她不是在问动画。
    猜你喜欢
    • 1970-01-01
    • 2016-10-07
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    相关资源
    最近更新 更多