0
votes

I'm trying to set up a tableview and I just keep running into problems, not the most experienced with them so some help would be great, heres the storyboard view and here's my code for the linked view controller

//
//  UpgradesTest.swift
//  myProject
//
//  Created by fgstu on 4/19/16.
//  Copyright © 2016 AllenH. All rights reserved.
//


import UIKit

class UpgradesTest: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

@IBOutlet var shopButton: UIButton!

@IBOutlet weak var shopLabel: UILabel!


var shopData: [MyData] = []


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    tableView.dataSource = self
    tableView.delegate = self

    shopData = [
        MyData(shopItemData: "Item 1", shopItemPrice: 1),
        MyData(shopItemData: "Item 2", shopItemPrice: 2),
        MyData(shopItemData: "Item 3", shopItemPrice: 3)
    ]
}

struct MyData {
    var shopItemData:String
    var shopItemPrice:Int
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    print(shopData[indexPath.row])
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Shop", forIndexPath: indexPath)

    cell.textLabel?.text = shopData[indexPath.row].shopItemData

    cell.shopLabel.text = shopData[indexPath.row].shopItemData

    cell.shopButton.label = shopData[indexPath.row].shopItemPrice

    return cell
}



}

The 4 errors are:

value of type 'UITableViewCell' as no member 'shopLabel'
value of type 'UITableViewCell' as no member 'shopButton'
the shopLabel outlet from the upgradesTest to the UILabel is invalid. Outlets cannot be connected to repeating content. the shopButton outlet from the upgradesTest to the UILabel is invalid. Outlets cannot be connected to repeating content.

and in the code my

cell.shopLabel.text = shopData[indexPath.row].shopItemData

cell.shopButton.label = shopData[indexPath.row].shopItemPrice

lines aren't working, any help?

1
Your outlets (shopButton and shopLabel) are not on your cell, they're on your UIViewController subclass. You need a custom cell subclass if you plan on adding subviews like this. As it stands, you put those properties in the wrong place. - Dare
fixed the outlets, now what would be the right line of code to change the text on the shopLabel with it in it's custom cell named "Shop" - Allen Huskins
Your custom cell needs to be registered with a reuse identifier and you need to cast the return value of dequeueReusableCellWithIdentifier() as the appropriate class in cellForRowAtIndexPath. Without those steps you are still acting on an instance of a UITableViewCell which does not have those properties. I would definitely take a step back and read up on inheritance before proceeding from this point. - Dare

1 Answers

0
votes

You are missing a key concept. When you have cells in a table and you are adding your own labels and fields to that cell, you have to make a new class that is derived from UITableViewCell. After you do that, in Storyboard, click the cell and use the Identity Inspector tab to tell it that it must use this new class instead of the default.

Connect outlets to the cell, not the view.

class MyCell : UITableViewCell {
    // put outlets here
}

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

    cell.textLabel?.text = shopData[indexPath.row].shopItemData
    cell.shopLabel.text = shopData[indexPath.row].shopItemData
    cell.shopButton.label = shopData[indexPath.row].shopItemPrice

    return cell
}