Outlets和UITableView有问题

时间:2016-04-20 17:37:28

标签: ios swift uitableview iboutlet

我试图建立一个桌面视图,我只是遇到问题,而不是最有经验的人,所以一些帮助会很棒,继承故事板 view 这是我的链接视图控制器代码

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



}

4个错误是:

  

类型' UITableViewCell的价值'没有会员&shop;但是没有会员   类型的值' UITableViewCell'没有会员&shop; Button'
  从升级测试到UILabel的shopLabel出口无效。   插座无法连接到重复内容。 shopButton   从升级测试到UILabel的出口无效。奥特莱斯不能   与重复内容相关联。

并在代码中

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

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

行不起作用,有什么帮助?

1 个答案:

答案 0 :(得分:0)

你错过了一个关键概念。当您在表中有单元格并且要将自己的标签和字段添加到该单元格时,您必须创建一个派生自UITableViewCell的新类。执行此操作后,在Storyboard中单击该单元格并使用Identity Inspector选项卡告诉它必须使用此新类而不是默认类。

将出口连接到单元格,而不是视图。

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
}