如何在swift中将数据设置为同一类中的第二个tableview

如何在swift中将数据设置为同一类中的第二个tableview,swift,Swift,如何在swift中将数据设置为同一类中的第二个tableview。我在同一控制器中使用两个表,一个用于下拉,另一个用于列表。我无法将数据设置为类中的第二个表(列表) 在cellForRowAtIndexPath中不调用as else部分。提前谢谢 import UIKit class PunchClockVC: UIViewController , UITableViewDataSource, UITableViewDelegate{ var appdel = UIApplicatio

如何在swift中将数据设置为同一类中的第二个tableview。我在同一控制器中使用两个表,一个用于下拉,另一个用于列表。我无法将数据设置为类中的第二个表(列表) 在cellForRowAtIndexPath中不调用as else部分。提前谢谢

import UIKit
class PunchClockVC: UIViewController , UITableViewDataSource, UITableViewDelegate{
     var appdel = UIApplication.sharedApplication().delegate as! AppDelegate

    @IBOutlet weak var dropdownTable: UITableView!
    @IBOutlet weak var mainTable: UITableView!
 override func viewDidLoad() {
        super.viewDidLoad()
        self.mainTable.registerClass(PunchClockCustomCell.self, forCellReuseIdentifier: "PunchClockCustomCell")
        self.dropdownTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "dropdowncell")
        self.dropdownTable.hidden = true
                }
 @IBAction func textFieldTapped(sender: AnyObject) {
        if self.dropdownTable.hidden == true {
            self.dropdownTable.hidden = false
        }
        else{
            self.dropdownTable.hidden = false
        }
    }

       func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == dropdownTable {
        return jobArrayID.count
    }

    return 8

}


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

    if tableView == self.dropdownTable {

         let cell = tableView.dequeueReusableCellWithIdentifier("dropdowncell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text =  (jobArrayID[indexPath.row] as! String) + "-" + (jobArrayName[indexPath.row] as! String)

        return cell

    }


 else {
        let cell1 = tableView.dequeueReusableCellWithIdentifier("PunchClockCustomCell", forIndexPath: indexPath) as! PunchClockCustomCell

        if indexPath.row == 0
        {
            cell1.jobcell?.font = UIFont(name: "MuseoSlab-500", size: 25.0)
            cell1.locationcell?.font = UIFont(name: "MuseoSlab-500", size: 25.0)
            cell1.timecell?.font = UIFont(name: "MuseoSlab-500", size: 25.0)
            cell1.typecell?.font = UIFont(name: "MuseoSlab-500", size: 25.0)


            cell1.jobcell?.textColor = UIColor.blackColor()
            cell1.locationcell?.textColor = UIColor.blackColor()
            cell1.timecell?.textColor = UIColor.blackColor()
            cell1.typecell?.textColor = UIColor.blackColor()


            cell1.jobcell?.text = "Job"
            cell1.locationcell?.text = "Location"
            cell1.timecell?.text = "Time"
            cell1.typecell?.text = "Type"
          //  return cell1

        }
        else {
            cell1.jobcell?.text = "Jobdata"
            cell1.locationcell?.text = "Locationdata"
            cell1.timecell?.text = "Timedata"
            cell1.typecell?.text = "OUT"
          //  return cell1

        }

        return cell1
    }

}

非常简单,您只需使用CellForRowatineXpath方法进行设置,但需要做的主要工作是编写如下代码

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    if tableView == firstTbaleView 
//outlet given to first tableView
   {
  let cell = tableView.dequeueReusableCellWithReuseIdentifier("cell1", forIndexPath: indexPath) as! cust1TableViewCell

            cell.imgView.image = images[indexPath.row]
            cell.filtLabel.text  = self.filtersCount[indexPath.row]


            return cell
    }}else {
            let cell2 = tableView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath) as! cust2TableViewCell
            cell2.imgview.image = UIImage(named: colPhotos[indexPath.row])
            cell2.labl.text = colNames[indexPath.row]

//            cell2.layer.borderColor = UIColor.blueColor().CGColor
//            cell2.layer.borderWidth = 2.0
            return cell2


}

就这样,您可以向我寻求任何帮助。

非常简单,您只需要使用CellForRowatineXpath方法设置它,但主要要做的是您需要像下面这样编写代码

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    if tableView == firstTbaleView 
//outlet given to first tableView
   {
  let cell = tableView.dequeueReusableCellWithReuseIdentifier("cell1", forIndexPath: indexPath) as! cust1TableViewCell

            cell.imgView.image = images[indexPath.row]
            cell.filtLabel.text  = self.filtersCount[indexPath.row]


            return cell
    }}else {
            let cell2 = tableView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath) as! cust2TableViewCell
            cell2.imgview.image = UIImage(named: colPhotos[indexPath.row])
            cell2.labl.text = colNames[indexPath.row]

//            cell2.layer.borderColor = UIColor.blueColor().CGColor
//            cell2.layer.borderWidth = 2.0
            return cell2


}

就这样,你可以向我寻求任何帮助。

让我们更安全一点:

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

    if tableView == self.dropdownTable {
        return jobArray.count
    }
    else if tableView == self.mainTable {
        return =  5
    }
    //Default return 0. This way if references are broken or change, you won't crash
    return 0
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if tableView == self.dropdownTable {
        let cell = tableView.dequeueReusableCellWithIdentifier("dropdowncell", forIndexPath: indexPath) as! UITableViewCell
        //configure your cell
        return cell
    }
    else if tableView == self.mainTable {
        let cell = tableView.dequeueReusableCellWithIdentifier("PunchClockCustomCell", forIndexPath: indexPath) as! PunchClockCustomCell
        //configure your cell
        return cell
    }
    //Shouln't ever reach here, but again, if we refactor somewhere then we'll see an error show up before here.
    return UITableViewCell()
}

让我们更安全一点:

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

    if tableView == self.dropdownTable {
        return jobArray.count
    }
    else if tableView == self.mainTable {
        return =  5
    }
    //Default return 0. This way if references are broken or change, you won't crash
    return 0
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if tableView == self.dropdownTable {
        let cell = tableView.dequeueReusableCellWithIdentifier("dropdowncell", forIndexPath: indexPath) as! UITableViewCell
        //configure your cell
        return cell
    }
    else if tableView == self.mainTable {
        let cell = tableView.dequeueReusableCellWithIdentifier("PunchClockCustomCell", forIndexPath: indexPath) as! PunchClockCustomCell
        //configure your cell
        return cell
    }
    //Shouln't ever reach here, but again, if we refactor somewhere then we'll see an error show up before here.
    return UITableViewCell()
}


你有什么错误吗?您还需要确保您具有处理
numberOfRowsInSection
的功能。您确定主表的数据源设置正确吗?正如@PhillipMills所说,这里有一个示例,检查您的数据源,由于我在当前视图控制器中看不到任何名为
jobArray
的数组,因此这两个表都设置了数据源和代理您是否收到任何错误?您还需要确保您具有处理
numberOfRowsInSection
的功能。您确定主表的数据源设置正确吗?正如@PhillipMills所说,这里有一个示例,检查您的数据源,因为我在当前视图控制器中看不到任何名为
jobArray
的数组,所以两个表都是用数据源设置的,并且为“maintable”调用delegatesCellForRowIndexPath,但数据未显示在tableviewNever中。。如果视图中有两个表,并且要为行声明cellfor,则它将为这两个表运行。这是委托方法。cellforRow方法没有主/辅。唯一需要做的是为不同的表设置条件。两个表都调用cellForRowAtIndexPath,但我没有在MainTable中获取数据。如果tableView==FirstBaleView,您是否给出了此条件?是。默认情况下,我必须加载maintable,所以我将其放入了else部分。如果tableView==下拉列表{//do this}或者{//do this},则为“maintable”调用CellForRowatineXpath,但数据不会显示在tableView中。。如果视图中有两个表,并且要为行声明cellfor,则它将为这两个表运行。这是委托方法。cellforRow方法没有主/辅。唯一需要做的是为不同的表设置条件。两个表都调用cellForRowAtIndexPath,但我没有在MainTable中获取数据。如果tableView==FirstBaleView,您是否给出了此条件?是。默认情况下,我必须加载maintable,所以我将其放入了else部分。如果tableView==下拉列表{//do this}或者{//do this}。