2016-03-30 20 views
0

Ich habe einen seltsamen Fehler in meinem UITableView. Ich habe zwei Zellen, SummaryHeaderTableViewCell und MapTableViewCell. Sie sind 400 und 200 Pixel groß. Ich habe zwei Reihen, und die erste Reihe sollte die Zusammenfassungszelle sein, und die zweite sollte Kartenzelle sein.Merkwürdiges UITableView-Verhalten

Aber immer noch die Ansichten im Simulator zeigt wie folgt aus:

enter image description here

Hier ist der Code:

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

    let cell: UITableViewCell 

    if indexPath == 0 { 

     let summaryCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SummaryHeaderTableViewCell 

     summaryCell.nameLabel.text = detailItem!["navn"] as? String 
     summaryCell.addressLabel.text = "\(detailItem!["adrlinje1"]), \(detailItem!["adrlinje2"])" 
     summaryCell.cityLabel.text = detailItem!["poststed"] as? String 

     let inspectionDate: String = detailItem!["dato"] as! String 
     summaryCell.inspectionDateLabel.text = self.convertDateString(inspectionDate) 

     cell = summaryCell 
    } 
    else 
    { 
     let mapCell = tableView.dequeueReusableCellWithIdentifier("MapCell", forIndexPath: indexPath) as! MapTableViewCell 

     // Set map options 

     cell = mapCell 
    } 

    return cell 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 2 
} 

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if (indexPath.section == 0) { 
     return 400.0 
    } else { 
     return 200.0 
    } 
} 

Hier ist das Storyboard mit dynamischen Prototypen:

enter image description here

Antwort

2

Sie sollte verweisen statt Abschnitt in heightForRowAtIndexPath rudern:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if (indexPath.row == 0) { 
     return 400.0 
    } else { 
     return 200.0 
    } 
} 
+0

Aaah, stupid me .. –

1

Sie nur ein einziges section im UITableView aufweist, und die verschiedenen Elemente auf zwei verschiedene Reihen sind besiedelt, nicht Abschnitte.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if indexPath.row == 0 { 
     let summaryCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SummaryHeaderTableViewCell 
     summaryCell.nameLabel.text = detailItem!["navn"] as? String 
     summaryCell.addressLabel.text = "\(detailItem!["adrlinje1"]), \(detailItem!["adrlinje2"])" 
     summaryCell.cityLabel.text = detailItem!["poststed"] as? String 
     let inspectionDate: String = detailItem!["dato"] as! String 
     summaryCell.inspectionDateLabel.text = self.convertDateString(inspectionDate) 
     return summaryCell 
    } 
    else 
    { 
     let mapCell = tableView.dequeueReusableCellWithIdentifier("MapCell", forIndexPath: indexPath) as! MapTableViewCell 
     // Set map options 
     return mapCell 
    } 
} 

Auch

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if (indexPath.row == 0) { 
     return 400.0 
    } 
    else { 
     return 200.0 
    } 
} 
+0

Dumme mir, natürlich. –

Verwandte Themen