2017-04-19 6 views
0

Ich habe zwei benutzerdefinierte Zellen, aber da ich keine statischen Höhen für diese Zellen einstellen kann, muss die erste Zelle eine Höhe von 100 haben, aber jede andere Zelle eine Höhe von 40. Der folgende Code macht alle Zellen eine Höhe von 100 statt nur der ersten.Wie habe ich zwei separate Zellen mit zwei verschiedenen Höhen?

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     if(indexPath.row == 0) { 
      return 100.0 
     } 
      return 40.0 
    } 
+0

Dieser Beitrag erwähnt nichts über Höhen. – joethemow

+1

Joe sehen die zweite verlinkt dupliziert: http://stackoverflow.com/questions/39071603/swift-how-to-set-dynamic-cell-height-in-tableviewcontroller- which- enthält -mehr – JAL

Antwort

0

Sie können Ihre erste Zelle in einen anderen Abschnitt einfügen.

func numberOfSections(in tableView: UITableView) -> Int { 
    return 2 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if section == 0 { 
     return 1 
    }else { 
     return yourArray.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if indexPath.section == 1 { 
     let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! FirstCustomCell 
     return cell 
    }else{ 
     let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! SecondCustomCell 
     return cell 
} 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    if indexPath.section == 1 { 
     return 100 
    } else{ 
     return 40 
} 
Verwandte Themen