2016-06-30 6 views
1

Недавно я столкнулся с проблемой.ViewController не соответствует протоколу UITableViewDataSource

enter image description here

я выложу код тоже. Пробовал методы из Интернета, но безуспешно. Кроме того, я связал 2 таблицы с контроллером представления.

enter image description here

Вот код.

class ViewController2: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet weak var carImageView: UIImageView! 
    @IBOutlet weak var pieseDorite: UITableView! 
    @IBOutlet weak var pieseDisponibile: UITableView! 


    var fullNameArr : [String] = [] // i populate the string in a action button, no problem with the arrays 
    var fullNameArrDorit : [String] = [] 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
     carImageView.image = UIImage(named: ("simplu")) 
     carImageView.contentMode = .ScaleAspectFit 

     pieseDisponibile.delegate = self 
     pieseDisponibile.dataSource = self 
     self.view.addSubview(pieseDisponibile) 

     pieseDorite.delegate = self 
     pieseDorite.dataSource = self 
     self.view.addSubview(pieseDorite) 
    } 


    func tableView(pieseDisponibile: UITableView, numberOfRowsInSection section:Int) -> Int 
    { 
       return fullNameArr.count 
    } 

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

     let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell") 
     cell.textLabel!.text = String(fullNameArr[indexPath.row]) 
     return cell 
    } 

    func tableView2(pieseDorite: UITableView, numberOfRowsInSection section:Int) -> Int 
    { 
     return fullNameArrDorit.count 
    } 

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

     let cell2:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell2") 
     cell2.textLabel!.text = String(fullNameArrDorit[indexPath.row]) 
     return cell2 
    } 

} 

Кроме того, при попытке добавить все другие функции проблема осталась. Я думаю, что я связал что-то плохое, я не знаю.

+0

заменить все tableview2 к Tableview, Вы можете определить TableView через тег в тех же методов, источников данных и делегат –

ответ

2

Замените все tableView2 на tableView, пожалуйста, и оставляйте только 1 способ. Вам нужны те же методы делегирования для всех экземпляров UITableView. Вы можете разделить разные экземпляры внутри этих методов, сравнив первый параметр с свойством UITableView. Кроме того, добавить номер метода раздела:

override func viewDidLoad() 
{ 
    super.viewDidLoad() 
    carImageView.image = UIImage(named: "simplu") 
    carImageView.contentMode = .ScaleAspectFit 

    pieseDisponibile.delegate = self 
    pieseDisponibile.dataSource = self 
    view.addSubview(pieseDisponibile) 

    pieseDorite.delegate = self 
    pieseDorite.dataSource = self 
    view.addSubview(pieseDorite) 
} 

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{ 
    if tableView == pieseDisponibile {return fullNameArr.count} 
    return fullNameArrDorit.count 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    if tableView == pieseDisponibile { 
     let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell") 
     cell.textLabel?.text = String(fullNameArr[indexPath.row]) 
     return cell 
    } 
    let cell2 = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell2") 
    cell2.textLabel?.text = String(fullNameArrDorit[indexPath.row]) 
    return cell2 
} 
+1

Пробовал, что я и раньше, но он не работал, поэтому я пошел для функций tableView и tableView2. Не знаю, что я сделал неправильно, но, видимо, ваш код отлично работал над моим проектом и решил мой проект. –

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