tableview没有在单元格Swift中正确加载数据

时间:2016-08-01 16:41:07

标签: swift null swift2 tableview tableviewcell

我正在创建一个将数据从RecipiesViewController推送到TableViewController2的应用程序。 TableViewController2中的单元格使用TableViewCell2类。由于某些原因,单元格没有正确加载,而不是显示我输入的信息,它们只是显示空白。我一直试图找到错误一段时间,但由于一些奇怪的原因,我尝试的一切只是提出了同样的问题。这是我的TableViewController2类:

import UIKit

struct Recipe2 {
let name: String
let thumbnails: String
let description: String
}

class TableViewController2: UITableViewController {

var firstRecipe: Recipe?
var recipies2 = [Recipe2]()
let identifier: String = "Cell2"

var checked = [Bool]()
var toPass:String!
var lightPressed:String!

override func viewDidLoad()
{
    super.viewDidLoad()

    navigationItem.title =  (firstRecipe?.name)! + " Instruments"
    initializeTherecipies2()



    //            InstrumentTableCell.backgroundColor = UIColor.whiteColor()

}

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

    return recipies2.count
}



func initializeTherecipies2()
{
    if (firstRecipe?.name)! == "Disinfectant"
    {
            self.recipies2 = [Recipe2(name: "Instrument", thumbnails: "", description: ""),
                             Recipe2(name: "AMI Codes-II", thumbnails: "AMI Codes II.jpg", description: "A-25.441.100.0"),
                             Recipe2(name: "Optional addon", thumbnails: "", description: ""),
                             Recipe2(name: "Reagent Set DPD", thumbnails: "", description: "A-85.410.120"),
                             Recipe2(name: "pH&T Option Codes", thumbnails: "", description: "A-87.127.020"),
                             Recipe2(name: "pH Cal set", thumbnails: "", description: "A-85.119.010"),
                             Recipe2(name: "manual", thumbnails: "", description: "A‐96.250.571"),
                             Recipe2(name: "Instrument", thumbnails: "", description: ""),
                             Recipe2(name: "AMI Codes-II TC", thumbnails: "AMI Codes II CC.jpg", description: "A-25.441.700.0"),
                             Recipe2(name: "Optional addon", thumbnails: "", description: ""),
                             Recipe2(name: "Reagent set, DPD", thumbnails: "", description: " A-85.410.120"),
                             Recipe2(name: "Reagent set, KI", thumbnails: "", description: "A-85.419.200"),
                             Recipe2(name: "pH&T Option Codes", thumbnails: "", description: "A-87.127.020"),
                             Recipe2(name: "pH Cal set", thumbnails: "", description: "A-85.119.010"),
                             Recipe2(name: "manual", thumbnails: "", description: "A‐96.250.631"),
                             Recipe2(name: "Instrument", thumbnails: "", description: ""),
                             Recipe2(name: "AMI Codes-II CC", thumbnails: "AMI Codes II TC.jpg", description: "A-25.441.600.0"),
                             Recipe2(name: "Optional addon", thumbnails: "", description: ""),
                             Recipe2(name: "Reagent set, DPD", thumbnails: "", description: "A-85.410.120"),
                             Recipe2(name: "Reagent set, KI", thumbnails: "", description: "A-85.419.200"),
                             Recipe2(name: "pH&T Option Codes", thumbnails: "", description: "A-87.127.020"),
                             Recipe2(name: "pH Cal set", thumbnails: "", description: "A-85.119.010"),
                             Recipe2(name: "manual", thumbnails: "", description: "A‐96.250.581")]



            self.tableView.reloadData()
        }

    else if (firstRecipe?.name)! == "Oxygen"
    {
        self.recipies2 = [Recipe2(name: "Chlorine", thumbnails: "Chlorine.jpg", description: "Description"),
                         Recipe2(name: "Water", thumbnails: "water.jpg", description: "Description"),
                         Recipe2(name: "Oxygen", thumbnails: "Oxygen.png", description: "Description"),
                         Recipe2(name: "Swan", thumbnails: "Swan.gif", description: "Description"),
                         Recipe2(name: "Element", thumbnails: "PerioticTable.jpg", description: "Description"),
                         Recipe2(name: "H2O", thumbnails: "H2O.png", description: "Description"),
                         Recipe2(name: "Starbucks Coffee", thumbnails: "starbucks_coffee.jpg", description: "Description")]

        self.tableView.reloadData()

    }
    else if (firstRecipe?.name)! == "Conductivity"
    {
        self.recipies2 = [Recipe2(name: "[[[AMI Turbiwell]]]", thumbnails: "AMITurbiwell.jpg", description: "A-209.123"),
                         Recipe2(name: "[[[Swansensor]]]", thumbnails: "Swansensor.jpg", description: "A-720.020")]

        self.tableView.reloadData()
    }
    else if (firstRecipe?.name)! == "ISE"
    {
        self.recipies2 = [Recipe2(name: "AMI Turbiwell", thumbnails: "AMITurbiwell.jpg", description: "A-209.123"),
                         Recipe2(name: "Swansensor", thumbnails: "Swansensor.jpg", description: "A-720.020")]

        self.tableView.reloadData()
    }
    else if (firstRecipe?.name)! == "Phosphate"
    {
        self.recipies2 = [Recipe2(name: "AMI Turbiwell", thumbnails: "AMITurbiwell.jpg", description: "A-209.123"),
                         Recipe2(name: "Swansensor", thumbnails: "Swansensor.jpg", description: "A-720.020")]

        self.tableView.reloadData()
    }
    else if (firstRecipe?.name)! == "SAC254"
    {
        self.recipies2 = [Recipe2(name: "AMI Turbiwell", thumbnails: "AMITurbiwell.jpg", description: "A-209.123"),
                         Recipe2(name: "Swansensor", thumbnails: "Swansensor.jpg", description: "A-720.020")]

        self.tableView.reloadData()
    }
    else if (firstRecipe?.name)! == "Turbidity"
    {
        self.recipies2 = [Recipe2(name: "AMI Turbiwell", thumbnails: "AMITurbiwell.jpg", description: "A-209.123"),
                         Recipe2(name: "Swansensor", thumbnails: "Swansensor.jpg", description: "A-720.020")]

        self.tableView.reloadData()
    }
    else if (firstRecipe?.name)! == "pH/ORP"
    {
        self.recipies2 = [Recipe2(name: "AMI Turbiwell", thumbnails: "AMITurbiwell.jpg", description: "A-209.123"),
                         Recipe2(name: "Swansensor", thumbnails: "Swansensor.jpg", description: "A-720.020")]

        self.tableView.reloadData()
    }



}
override func tableView(tableView: UITableView,
                        heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    let cell = tableView.dequeueReusableCellWithIdentifier("instrumentTableCell") as! InstrumentTableCell!

    if indexPath.row == 1 {//should be if cell.backgroundcolor or something
        return 50//noncolored cells
    } else {
        return 50 // colored cells
    }
}
// MARK: - UITableView DataSource

//        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//            return recipies2.count
//        }


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cellIdentifier = "Cell2"
    let cell: TableViewCell2! = tableView.dequeueReusableCellWithIdentifier(identifier) as? TableViewCell2

    checked = Array(count:recipies2.count, repeatedValue:false)
   // cell.configurateTheCell(recipies2[indexPath.row])

    if !checked[indexPath.row] {
        cell.accessoryType = .None
    } else if checked[indexPath.row] {
        cell.accessoryType = .Checkmark
    }


    return cell
}

//    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
//        if editingStyle == UITableViewCellEditingStyle.Delete {
//            recipes.removeAtIndex(indexPath.row)
//            self.tableView?.reloadData()
//            self.tableView?.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Right)
//        }
//    }

//MARK: - UITableView Delegate
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        if cell.accessoryType == .Checkmark {
            cell.accessoryType = .None
            checked[indexPath.row] = false
        } else {
            cell.accessoryType = .Checkmark
            checked[indexPath.row] = true
        }
    }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "segue2") {
        let seg = segue.destinationViewController as! thirdTableViewController
        var arr: [Recipe2] = [Recipe2]()
        for index in 0..<recipies2.count {
            if checked[index] {
                arr.append(self.recipies2[index])
            }
        }
        seg.catagory = toPass
        seg.selectedRecipes = arr
    }

}
}

这是我的TableViewCell2类:

import Foundation
import UIKit

class TableViewCell2: UITableViewCell {

@IBOutlet var nameLabel: UILabel?
@IBOutlet var descriptionLabel: UILabel?
@IBOutlet var thumbnailImage: UIImageView!

func configurateTheCell(recipie: Recipe2) {
    self.nameLabel?.text = recipie.name
    self.descriptionLabel?.text = recipie.description
    self.thumbnailImage?.image = UIImage(named: recipie.thumbnails)
    if nameLabel!.text == "Instrument"
    {
        backgroundColor = UIColor.orangeColor()
        nameLabel?.textColor = UIColor.blackColor()
        userInteractionEnabled = false
    }
    else if nameLabel!.text == "Optional addon"
    {
        backgroundColor = UIColor.cyanColor()
        nameLabel?.textColor = UIColor.blackColor()
        userInteractionEnabled = false
    }
    else
    {
        backgroundColor = UIColor.whiteColor()
        nameLabel?.textColor = UIColor.redColor()
        userInteractionEnabled = true
    }

}

}

0 个答案:

没有答案