2016-06-04 20 views
6

Tôi đang cố gắng để animate chiều cao của hàng tableViewCell bằng cách gọi startAnimation() bên trong hàm tableView:Swift: Làm thế nào để animate rowHeight của một UITableView?

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!") 
    }) 
} 

Kết quả: Chiều cao hàng không đổi nhưng không có bất kỳ hình ảnh động xảy ra. Tôi không hiểu tại sao hoạt ảnh không hoạt động. Tôi có nên định nghĩa một số trạng thái bắt đầu và kết thúc ở đâu đó không?

Trả lời

11

Không thay đổi chiều cao theo cách đó. Thay vào đó, khi bạn biết bạn muốn thay đổi chiều cao của ô, hãy gọi (trong bất kỳ chức năng nào):

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

Các cuộc gọi này thông báo cho bảngXem để kiểm tra thay đổi độ cao. Sau đó, thực hiện ủy quyền override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat và cung cấp chiều cao thích hợp cho từng ô. Thay đổi về chiều cao sẽ được tự động làm động. Bạn có thể trả lại UITableViewAutomaticDimension cho các mục bạn không có chiều cao rõ ràng.

Tôi sẽ không đề xuất thực hiện các hành động như vậy từ bên trong cellForRowAtIndexPath, tuy nhiên, nhưng trong một phản ứng với một lần nhấn didSelectRowAtIndexPath chẳng hạn. Trong một trong các lớp học của tôi, tôi làm:

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
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

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

    tableView.rowHeight = 44.0 

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

     tableView.rowHeight = 88.0 
     cell.layoutIfNeeded() 

    }, completion: { finished in 

     print("Row heights changed!") 
    }) 

    return cell 
} 
Các vấn đề liên quan