使用cellForRowAtIndexPath

时间:2015-08-08 18:07:38

标签: ios swift tableview

我有两个数据结构(都有一个Category项)。我正在使用numberOfRowsInSection来确定具有特定类别的行数(基于使用上一个屏幕中的prepareForSegue传递的索引)。

numberOfRowsInSection函数正在工作(即屏幕显示正确的行数)。

现在我想在每一行上显示正确的内容。

我在cellForRowAtIndexPath中的逻辑似乎不正确,因为在某些情况下,cell.vendorName.text显示正确的数据,而在其他情况下,它显示“无值”。

我如何调整以下代码,以便仅显示与上一屏幕中选择的特定类别类型相关的数据。

class VendorFetchTableViewController: UITableViewController {

let categoryData = Data().headerData
let vendorData = Data().vendorData

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillAppear(animated: Bool) {

}

var index: Int?


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // warning incomplete implementation, return the number of sections
    return 1
}

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

    let categoryData = Data().headerData
    let vendorData = Data().vendorData

    let category = categoryData[index!]
    let categoryType = category.category

    var count: Int = 0

    for (var i=0; i < vendorData.count; i++) {

        if vendorData[i].category == categoryType {

            count = count + 1
        }
    }

    return count
 }


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("vendorCell", forIndexPath: indexPath) as! VendorSelectionTableViewCell

    let category = categoryData[index!]
    let categoryType = category.category


        if vendorData[indexPath.row].category == categoryType {
            //the UILabel for VendorName is in a UITableViewCell file which is connected to the Prototype Cell in the TableViewController
            cell.vendorName.text = vendorData[indexPath.row].name
            print(vendorData[indexPath.row].category)
            print(categoryType)
        } else {
            cell.vendorName.text = "No Value"
        }

    return cell
}

}

以下是Data.swift文件:

class Data {

class Entry {

    let filename: String
    let heading: String
    let headerSub: String
    let description: String
    let category: String

    init(profilePicture: String, profileTitle: String, profileSub: String, description: String, category: String) {
        self.heading = profileTitle
        self.filename = profilePicture
        self.headerSub = profileSub
        self.description = description
        self.category = category
    }

}

let headerData = [

    Entry(profilePicture: "sampleImage.png", profileTitle: "Title1", profileSub: "Sub1", description: "Desc1", category: "Category1"),
Entry(profilePicture: "sampleImage2.png", profileTitle: "Title2", profileSub: "Sub2", description: "Desc2", category: "Category2"),
Entry(profilePicture: "sampleImage3.png", profileTitle: "Title3", profileSub: "Sub3", description: "Desc3", category: "Category3"),
]


class Vendor {

    let pictureFilename: String
    let name: String
    let review: Double
    let reviewNumber: Int
    let address: String
    let cost: Int
    let category: String

    init(profilePicture: String, name: String, review: Double, reviewNumber: Int, address: String, cost: Int, category: String) {
        self.pictureFilename = profilePicture
        self.name = name
        self.review = review
        self.reviewNumber = reviewNumber
        self.address = address
        self.cost = cost
        self.category = category
    }
}

let vendorData = [

    Vendor(profilePicture: "vendor1.png", name: "name1", review: 4.7, reviewNumber: 100, address: "Add1", cost: 30, category: "Category1"),
     Vendor(profilePicture: "vendor2.png", name: "name2", review: 5.0, reviewNumber: 23, address: "Add2", cost: 35, category: "Category2"),
    Vendor(profilePicture: "vendor3.png", name: "name3", review: 4.7, reviewNumber: 100, address: "Add3", cost: 30, category: "Category3")


]
}

0 个答案:

没有答案