2015-12-20 8 views
0

Я пытаюсь сделать tableViewCell, что каждая из них имеет разную высоту. Я попытался объявить каждый из идентификаторов ячеек и его имя и разделить на случаи на используя «переключатель». И, во-вторых, я также хочу указать lable на mainViewCell, который находится в коде. Как я могу изменить каждую высоту ячеек?Как сделать пользовательский TableViewCell, который у каждого из них имеет разную высоту

import UIKit 

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
    @IBOutlet var tableView: UITableView! 
    override func viewDidLoad() { 
      super.viewdidload() 
     tableView.registerNib(UINib(nibName: "mainViewTableViewCell", bundle: nil), forCellReuseIdentifier: "mainViewCell") 
} 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     tableView.scrollEnabled = false 
     return 4 
    } 


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     switch tableView.numberOfRowsInSection(4) { 
      case 0: 
       let titleCell = tableView.dequeueReusableCellWithIdentifier("titleCell")! as UITableViewCell 
      return titleCell 
      case 1: 
       let wayCell = tableView.dequeueReusableCellWithIdentifier("whichWayCell")! as UITableViewCell 
       return wayCell 
      case 2: 
       let campusCell = tableView.dequeueReusableCellWithIdentifier("campusCell")! as UITableViewCell 
       return campusCell 
      default : 
        let mainViewCell = tableView.dequeueReusableCellWithIdentifier("mainViewCell")! as! mainViewTableViewCell 
        mainViewCell.mainViewLabel.text = onUpdate(timer) 
        return mainViewCell 

     } 
    } 
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    switch tableView.numberOfRowsInSection(4) { 
    case 0: 
     tableView.numberOfRowsInSection(1) 
    return 50 
    case 1: 
     tableView.numberOfRowsInSection(2) 
    return 50 
    case 2: 
     tableView.numberOfRowsInSection(3) 
    return 50 
    default: 
     tableView.numberOfRowsInSection(4) 
    return 300 
    } 
} 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
} 
+1

В чем проблема u r? –

+0

На самом деле, я хочу указать строку и изменить только одну высоту ячеек, но она будет зависеть от всех ячеек, чтобы подвести итог, я хочу настроить каждую ячейку разными способами, но настройка будет применяться ко всем ячейкам, и это это будет проблема – yanamstyle

+0

как насчет этого на раскадровке? – adolfosrs

ответ

1

Вы должны использовать indexPath перешла в UITableViewDataSource методов. Есть пара проблем с кодом. Имеется документация here.

switch tableView.numberOfRowsInSection(4) заявление в методе cellForRowAtIndexPath должно быть: switch(indexPath.section)

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    switch tableView.numberOfRowsInSection(4) { 
    case 0: 
     tableView.numberOfRowsInSection(1) 
    return 50 
    case 1: 
     tableView.numberOfRowsInSection(2) 
    return 50 
    case 2: 
     tableView.numberOfRowsInSection(3) 
    return 50 
    default: 
     tableView.numberOfRowsInSection(4) 
    return 300 
} 

Почему вы называете numberOfRowsInSection() ?, это должно быть оставлено на ОС для вызова. Попробуй это. Если вы хотите, чтобы высота ячейки таблицы отображалась в зависимости от раздела, измените значение switch(indexPath.row) на switch(indexPath.section).

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 

    var height : CGFloat = 0 

    switch(indexPath.row) { 
    case 0, 2, 3: 
     height = 50.0 
    case 4: 
     height = 300.0 
    default: 
     break 
    } 

    return height 

} 
+0

Жаль, что я все еще новичок, и мне казалось, что я не понимаю структуру и значения слов в таблице. Но благодаря вам, я мог бы решить проблему и узнать, что мне нужно изучать, спасибо. – yanamstyle

+0

Не беспокойтесь! Держись, ты доберешься;) –

Смежные вопросы