2016-06-04 12 views
6

मैं tableView समारोह अंदर startAnimation बुला() द्वारा tableViewCell पंक्तियों की ऊंचाई चेतन करने के लिए कोशिश कर रहा हूँ:स्विफ्ट: 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!") 
    }) 
} 

परिणाम: पंक्ति की ऊँचाई में बदल जाती है लेकिन होने वाली किसी भी एनीमेशन के बिना। मुझे समझ में नहीं आता कि एनीमेशन क्यों काम नहीं करता है। क्या मुझे शायद कुछ शुरुआत और अंत राज्य को परिभाषित करना चाहिए?

उत्तर

11

उस तरह की ऊंचाई को न बदलें। इसके बजाय, जब आप जानते हैं आप, कॉल (जो कुछ भी समारोह में) एक सेल की ऊंचाई को बदलना चाहते हैं:

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
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 
} 
संबंधित मुद्दे