Swift:如何为UITableView的rowHeight设置动画?

时间:2016-06-04 04:13:49

标签: swift uitableview animation

我试图通过调用tableView函数中的startAnimation()来设置tableViewCell行的高度动画:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TableViewCell

    tableView.rowHeight = 44.0

    startAnimation(tableView)

    return cell
}

//MARK: Animation function

func startAnimation(tableView: UITableView) {

    UIView.animateWithDuration(0.7, delay: 1.0, options: .CurveEaseOut, animations: {

        tableView.rowHeight = 88.0

    }, completion: { finished in

        print("Row heights changed!")
    })
}

结果:行高确实发生了变化,但没有发生任何动画。我不明白动画为什么不起作用。我是否应该在某处定义一些开始和结束状态?

2 个答案:

答案 0 :(得分:22)

不要改变那样的高度。相反,当您知道要更改单元格的高度时,请调用(在任何函数中):

self.tableView.beginUpdates()
self.tableView.endUpdates()

这些调用通知tableView检查高度变化。然后实现委托override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat,并为每个单元格提供适当的高度。高度的变化将自动动画。对于没有明确身高的项目,您可以返回UITableViewAutomaticDimension

我不会建议在cellForRowAtIndexPath内执行此类操作,但是在一个响应点按didSelectRowAtIndexPath的操作中。在我的一个课程中,我做了:

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

internal var selectedIndexPath: NSIndexPath? {
    didSet{
      //(own internal logic removed)

      //these magical lines tell the tableview something's up, and it checks cell heights and animates changes
      self.tableView.beginUpdates()
      self.tableView.endUpdates()
    }
  }

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath == self.selectedIndexPath {
      let size = //your custom size
      return size
    }else{
      return UITableViewAutomaticDimension
    }
  }

答案 1 :(得分:1)

Sub Format_ZM27KG()
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim X1 As Long
    Dim LookUpTable1 As Variant
    Dim LookUpValue1 As Long
    Dim LastRow1 As Long
    Dim vAnswer1 As String
    Dim X2 As Long
    Dim vAnswer2 As Long
    Dim LastRow2 As Long
    Dim vAnswer3 As Long

    Set ws1 = ActiveWorkbook.Worksheets("Format KG")
    Set ws2 = ActiveWorkbook.Worksheets("LookUp")

    Application.ScreenUpdating = False

    Dim A1 As Long
    For A1 = 1 To 8 Step 1
    ws1.Rows(1).EntireRow.Delete
    Next A1

    Dim LR3 As Long
    Dim i2 As Long
    With ws1
        LR3 = .Range("C" & .Rows.Count).End(xlUp).Row
        For i2 = LR3 To 2 Step -1
        If Not IsNumeric(.Range("C" & i2).Value) Or .Range("C" & i2).Value = "" Then .Rows(i2).Delete
        Next i2
    End With

    'Delete columns on tab format cases
    ws1.Columns("A:B").EntireColumn.Delete
    ws1.Columns("B:D").EntireColumn.Delete
    ws1.Columns("C:M").EntireColumn.Delete
    ws1.Columns("N").EntireColumn.Delete
    ws1.Columns("C").EntireColumn.Delete

    ws1.Cells(1, "N").Value = "Category"

    LastRow1 = ws1.Cells(Rows.Count, "A").End(xlUp).Row
    On Error Resume Next
    LookUpTable1 = ws2.Range("A1:C500")
    For X1 = 2 To LastRow1
    LookUpValue1 = Cells(X1, "A").Value
    vAnswer1 = Application.WorksheetFunction.VLookup(LookUpValue1, LookUpTable1, 3, False)
    ws1.Cells(X1, "N").Value = vAnswer1
    Next X1

    ws1.Columns("A:AL").AutoFit

    ws1.Rows(1).HorizontalAlignment = xlCenter

    ws1.Range("A1").Select
    Application.ScreenUpdating = True

End Sub