【问题标题】:Expand and Collapse tableview cells展开和折叠表格视图单元格
【发布时间】:2016-08-23 08:34:21
【问题描述】:

我可以展开和折叠单元格,但我想在 UITableViewCell 中调用函数(展开和折叠)来更改按钮标题。

导入 UIKit 类 MyTicketsTableViewController: UITableViewController { var selectedIndexPath: NSIndexPath? var extraHeight: CGFloat = 100 覆盖 func viewDidLoad() { super.viewDidLoad() } 覆盖 func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // 处理所有可以重新创建的资源。 } 覆盖 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 返回 5 } 覆盖 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 让 cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyTicketsTableViewCell 返回单元格 } 覆盖 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if(selectedIndexPath != nil && indexPath.compare(selectedIndexPath!) == NSComparisonResult.OrderedSame) { 返回 230 + 额外高度 } 返回 230.0 } 覆盖 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if(selectedIndexPath == indexPath) { selectedIndexPath = nil } 别的 { selectedIndexPath = indexPath } tableView.beginUpdates() tableView.endUpdates() } } 导入 UIKit 类 MyTicketsTableViewCell: UITableViewCell { @IBOutlet 弱 var expandButton:ExpandButton! @IBOutlet 弱变量 detailsHeightConstraint:NSLayoutConstraint! var defaultHeight: CGFloat! 覆盖 func awakeFromNib() { super.awakeFromNib() defaultHeight = detailsHeightConstraint.constant expandButton.button.setTitle("点击查看详细信息", forState: .Normal) detailsHeightConstraint.constant = 30 } 函数展开(){ UIView.animateWithDuration(0.3,延迟:0.0,选项:.CurveLinear,动画:{ self.expandButton.arrowImage.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 0.99)) self.detailsHeightConstraint.constant = self.defaultHeight self.layoutIfNeeded() },完成:{完成于 self.expandButton.button.setTitle("CLOSE", forState: .Normal) }) } 函数折叠(){ UIView.animateWithDuration(0.3,延迟:0.0,选项:.CurveLinear,动画:{ self.expandButton.arrowImage.transform = CGAffineTransformMakeRotation(CGFloat(M_PI * 0.0)) self.detailsHeightConstraint.constant = CGFloat(30.0) self.layoutIfNeeded() },完成:{完成于 self.expandButton.button.setTitle("点击查看详细信息", forState: .Normal) }) } }

【问题讨论】:

  • 请查看此解决方案:enter link description here
  • Swift 4.2 我以编程方式创建了一个示例可扩展表格视图。 - 扩展单元格出现和消失的动画。 - 所选部分的标题在展开时更改。 - 视图在代码中,不需要故事板。在下面的链接中查看:???? Gist of expandable table view

标签: ios swift uitableview


【解决方案1】:

如果你想让细胞变得更大,那么你有你的商店IndexPath,在heightForRow:使用:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if selectedIndexPath == indexPath {
      return 230 + extraHeight
    }
    return 230.0
}

那么当你想在 didSelectRow 中展开一个时:

selectedIndexPath = indexPath
tableView.beginUpdates
tableView.endUpdates

编辑

这将使单元格动画自己变得更大,您不需要单元格中额外的动画块。

编辑 2

 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if(selectedIndexPath == indexPath) {
          selectedIndexPath = nil

          if let cell = tableView.cellForRowAtIndexPath(indexPath) as? MyTicketsTableViewCell {
            cell.collapse()
          }
          if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow:indexPath.row+1, section: indexPath.section) as? MyTicketsTableViewCell {
            cell.collapse()
          }
        } else {
          selectedIndexPath = indexPath

          if let cell = tableView.cellForRowAtIndexPath(indexPath) as? MyTicketsTableViewCell {
              cell.expand()
          }

          if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow:indexPath.row+1, section: indexPath.section) as? MyTicketsTableViewCell {
             cell.expand()
          }
        }

        tableView.beginUpdates()
        tableView.endUpdates()
    }

【讨论】:

  • 我可以像你说的那样折叠和展开单元格。但我想更改单元格内的按钮标题,所以我如何在单元格内调用函数。
  • 两个单元格函数调用。我的意思是当我单击一个单元格时,两个单元格展开功能运行:(
  • cell.expand() 调用两次
  • 再次查看只需调用 indexPath + 1 处的每个单元格
【解决方案2】:

您只需要以这种方式实现UITableView Delegate:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

默认情况下,estimatedHeightCGRectZero,当您为其设置一些值时,它会启用自动调整大小(与UICollectionView 相同的情况),您甚至可以这样做:

tableView?.estimatedRowHeight = CGSizeMake(50.f, 50.f); 

然后你需要在你的单元格中设置你的约束。

查看此帖子:https://www.hackingwithswift.com/read/32/2/automatically-resizing-uitableviewcells-with-dynamic-type-and-ns

希望对你有帮助)

【讨论】:

  • 这应该是一条评论。
  • 将 UITableViewAutomaticDimension 用于estimateHeight 违背了使用estimatedHeight 的全部目的——它将在tableView 中的每个indexPath 上调用自动布局来计算整个内容的高度,而不是使用auto布局仅用于可见单元格,并为所有不可见的 indexPaths 估算高度(理想情况下计算速度要快得多)。
【解决方案3】:

MyTicketsTableViewController 类中,在cellForRowAtIndexPath 数据源方法中为按钮添加目标。

cell.expandButton.addTarget(self, action: "expandorcollapsed:", forControlEvents: UIControlEvents.TouchUpInside)

【讨论】:

  • 我不明白它是干什么用的?
  • 您在 tableviewcell 类中调用展开和折叠方法。取而代之的是,在您的 tableviewcontroller 中声明展开、折叠方法并在 cellForRowAtIndexPath 中调用这样的方法
【解决方案4】:

我尝试在此页面和其他页面上实现许多给定的示例,但没有一个对我有用,因为我必须在我的自定义单元格中执行一些逻辑(例如,当单元格时在 CustomCell.swift 中隐藏不需要的 UILables已折叠)。

这是对我有用的实现:

  1. 创建字典:

    private var _expandedCells: [IndexPath:Bool] = [:]
    
  2. 实现 heightForRowAt 方法:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return _expandedCells[indexPath] == nil ? 70 : _expandedCells[indexPath]! ? 150 : 70
    }
    
  3. 实现 didSelectRowAt 方法:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
        _expandedCells[indexPath] = _expandedCells[indexPath] == nil ? true : !_expandedCells[indexPath]!
    
        tableView.reloadRows(at: [indexPath], with: .fade)
    }
    
  4. 调整您的 customCell:

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    
        guard let cell = cell as? YourCustomTableViewCell, let isExpanded = _expandedCells[indexPath] else { return }
    
        cell.set(expanded: isExpanded)
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    相关资源
    最近更新 更多