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



如何在swift中将数据设置到同一类中的第二个表视图。我在同一个控制器中使用两个表,一个用于下拉,另一个用于列表。我无法将数据设置到类中的第二个表(列表)因为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
    }

}

这很简单。你只需要用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

}

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

让我们更安全一点:

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

最新更新