Allen Huskins Allen Huskins - 7 months ago 11
Swift Question

Outlets and UITableView trouble

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?

Answer

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
}
Comments