ios - 单击时折叠和展开 UI

标签 ios swift uitableview constraints

我希望我的 commentTable 在点击时展开,然后在第二次点击时折叠。

  var commentsInt = -1
   @IBOutlet weak var commentsTop: NSLayoutConstraint!
   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if commentsInt == -1
    {
        commentsInt = 1
    }
    if commentsInt == 0{

        commentsTop.constant = 8
        commentsInt = 1
    }

    if commentsInt == 1 {
        commentsTop.constant = -220
        commentsInt = 0

    }

}

这个函数可以扩展表格(虽然没有动画看起来有点笨拙),但我无法在第二次单击时收缩表格。

同样,我想在滚动时展开一个textView(您不能编辑这个名为mainText的textView)。所以我尝试了

var mainInt = -1
@IBOutlet weak var mainHeight: NSLayoutConstraint!
 func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if mainInt == -1
    {
        mainInt = 1
    }
    if mainInt == 0{

        mainHeight.constant = 440
        mainInt = 1
    }

    if mainInt == 1 {
        mainHeight.constant = 120
        mainInt = 0

    }
}

但是当我滚动时 mainText 根本不展开。我该如何解决这些问题?

最佳答案

认为您每次点击都会将高度设置两次。试试这个:

var commentsInt = -1
@IBOutlet weak var commentsTop: NSLayoutConstraint!

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  if commentsInt == -1
  {
      commentsInt = 1
  }
  if commentsInt == 0{

      commentsTop.constant = 8
      commentsInt = 1
  } else if commentsInt == 1 {
      commentsTop.constant = -220
      commentsInt = 0

  }

}

关于ios - 单击时折叠和展开 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57384126/

相关文章:

ios - 如何使Web服务通过NSURLConnection连接:didFailWithError:?传递错误

ios - 什么是用于扫描可用 Wi-Fi 网络的 iOS API

ios - AVPlayer "freezes"开始缓冲音频流的应用程序

json - 我如何在 Swift 中解码 JSON,它是一个包含双嵌套项目的数组?

ios - 应用程序恢复后 URLSession 代表不工作

ios - 防止为 STTweetLabel 调用 didSelectRowAtIndexPath

ios - 按下导航 Controller 左栏按钮时如何停止 View 消失

ios - 从另一个 UIViewController 的 UITableView 获取 UITableViewCell

ios - 如何在 iOS sdk 的 tableview 中左右滑动

ios - shouldChangeCharactersInRange 从 UITextField 返回第二个字符