2016-06-30 107 views
1

我最近遇到了一个问题。ViewController不符合协议UITableViewDataSource

enter image description here

我会后一些代码了。尝试从互联网的方法,但没有成功。另外,我已将这两个表格链接到View Controller。

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

请用tableView替换全部tableView2,并且每种方法只留下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功能。不知道我做错了什么,但显然你的代码在我的项目上完美工作,并解决了我的项目。 –

相关问题