2017-07-04 2 views
0

Ich habe ein seltsames Verhalten beim Aktualisieren eines TableViews Zellen.IOS Swift: Animieren TableViewCell Änderungen funktionieren nicht richtig

Der einfachste Weg zu beschreiben, ist ein Video: https://drive.google.com/open?id=0B67InGf2FEPaODFLUEhLZ29LWTg

Hier können Sie sehen, dass das erste Mal, dass ich versuchen (oder Kollaps), um die Ansicht zu erweitern, es sor von etwas tut, aber nicht wirklich. Die Ansichten, die ich zeigen wollte, sind nicht da, aber es flackert.

Hier ist mein Code:

class StylingTableViewController: UITableViewController { 

//MARK: properties 
var articles = [Article]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    articles.append(contentsOf: [Article(id: "Artikel 1"), Article(id: "Artikel 2")]) 

    //Let table auto layout in height 
    tableView.rowHeight = UITableViewAutomaticDimension 
    tableView.estimatedRowHeight = 100 

    // Use the edit button item provided by the table view controller. 
    navigationItem.rightBarButtonItem = editButtonItem 
} 

// MARK: - Table view data source 
override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return articles.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    // Table view cells are reused and should be dequeued using a cell identifier. 
    let cellIdentifier = "ArticleDetailCell" 

    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? StylingDetailTableViewCell else { 
     fatalError("The dequeued cell is not an instance of StylingDetailTableViewCell.") 
    } 

    let article = articles[indexPath.row] 
    cell.articleName.text = article.id 

    //Scrollview 
    let width:CGFloat = 90; 
    var xPos:CGFloat = 0; 
    var scrollViewContentSize:CGFloat=0; 
    for _ in 0...10{ 
     let myView:CFPictureView = CFPictureView() 
     myView.frame.size.width = 80 
     myView.frame.size.height = 120 
     myView.frame.origin.x = CGFloat(xPos) 
     xPos += width 
     cell.pictures_scrollView.addSubview(myView) 
     scrollViewContentSize += width 
     cell.pictures_scrollView.contentSize = CGSize(width: scrollViewContentSize, height: 120) 
    } 

    return cell 
} 

// Override to support editing the table view. 
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == .delete { 
     // Delete the row from the data source 
     articles.remove(at: indexPath.row) 
     tableView.deleteRows(at: [indexPath], with: .fade) 
    } else if editingStyle == .insert { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 

//MARK: Actions 
@IBAction func favButtonPressed(_ sender: UIButton) { 
    sender.isSelected = !sender.isSelected 
} 
@IBAction func expandCell(_ sender: UIButton) { 

    if let cell = sender.superview?.superview?.superview?.superview?.superview as? StylingDetailTableViewCell { 
     cell.fieldDescriptorStackView.isHidden = false; 
     cell.articleBarcode.isHidden = false; 
     cell.articleCustomerNumber.isHidden = false; 
     cell.articleStyle.isHidden = false; 
     cell.expandCellButton.isHidden = true; 
     cell.collapseCellButton.isHidden = false; 
     cell.pictures_order.isHidden = false; 
     cell.pictures_amount.isHidden = false; 
     cell.pictures_scrollView.isHidden = false; 
     cell.pictures_title.isHidden = false; 

     let indexPath = tableView.indexPath(for: cell) 
     self.tableView.reloadRows(at: [indexPath!], with: UITableViewRowAnimation.automatic) 
     (self.parent as? StylingViewController)?.topStackView.isHidden = true; 
    } 

} 
@IBAction func collapseCell(_ sender: UIButton) { 

    if let cell = sender.superview?.superview?.superview as? StylingDetailTableViewCell { 
     cell.fieldDescriptorStackView.isHidden = true; 
     cell.articleBarcode.isHidden = true; 
     cell.articleCustomerNumber.isHidden = true; 
     cell.articleStyle.isHidden = true; 
     cell.expandCellButton.isHidden = false; 
     cell.collapseCellButton.isHidden = true; 
     cell.pictures_order.isHidden = true; 
     cell.pictures_amount.isHidden = true; 
     cell.pictures_scrollView.isHidden = true; 
     cell.pictures_title.isHidden = true; 

     let indexPath = tableView.indexPath(for: cell) 
     self.tableView.reloadRows(at: [indexPath!], with: UITableViewRowAnimation.automatic) 
     (self.parent as? StylingViewController)?.topStackView.isHidden = false; 
    } 
} 
func undoAction() { 
    print("undo") 

} 

}

Es ist wie etwas mit tableView.reloadRows ist falsch zu sein scheint. Hilfe wäre wünschenswert

Antwort

0

Könnte das Problem sein, weil die App immer noch die geschätzte Höhe der Zelle beim ersten Mal wusste. Ein Update könnte durch das Hinzufügen dieser auf viewDidLoad sein:

tableView.estimatedRowHeight = 60 
tableView.rowHeight = UITableViewAutomaticDimension 
+0

Vielen Dank für Ihre Antwort, aber ich hatte schon tableView.rowHeight = UITableViewAutomaticDimension und tableView.estimatedRowHeight = 100 in meinem viewDidLoad() – Tterhuelle

+0

Könnten Sie Ihre Klassendatei teilen? –

+0

Ich habe die ganze Klassendatei hinzugefügt – Tterhuelle