Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
S B
4,497 Pointsdata not being inputted properly using cellForRowAtIndexPath
After completing the tableView section of the Swift course, I took it upon myself to expand my knowledge of how tables work in general. I've run into a slight problem which I'm hoping to get help with ...
I have two data structures (both with a Category item). I'm using numberOfRowsInSection to determine the number of rows with a specific category (based on an index passed using prepareForSegue from the previous screen). The numberOfRowsInSection function is working (i.e. the screen shows the correct number of rows). Now I'd like to actually display the correct content on each row. My logic in cellForRowAtIndexPath doesn't seem to be correct because in some cases cell.vendorName.text displays the correct data, and in other cases it displays "No Value". How would I tweak the following code in order to ONLY display data related to a particular category type which was selected on the previous screen.
var index: Int?
let categoryData = Data().headerData
let vendorData = Data().vendorData
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
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
let vendorEntry = vendorData[indexPath.row]
print(vendorEntry.category)
if vendorEntry.category == categoryType {
cell.vendorName.text = vendorData[indexPath.row].name
} else {
cell.vendorName.text = "No Value"
}
return cell
}
Thank you!