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

时间:2016-07-27 13:24:25

标签: swift

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

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
    }

}

2 个答案:

答案 0 :(得分:0)

这很简单你只需要用cellForRowAtIndexPath方法设置它,但主要做的是你需要像下面那样编码

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


}

就是这样你可以向我求助......

答案 1 :(得分:0)

让我们更安全一点:

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()
}