Outlets 和 UITableView 的麻烦
Outlets and UITableView trouble
我正在尝试设置一个表格视图,我只是 运行 遇到问题,而不是最有经验的人所以一些帮助会很好,这是情节提要
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 个错误是:
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.
在我的代码中
cell.shopLabel.text = shopData[indexPath.row].shopItemData
cell.shopButton.label = shopData[indexPath.row].shopItemPrice
线路坏了,有什么帮助吗?
您缺少一个关键概念。当您在 table 中有单元格并且要向该单元格添加自己的标签和字段时,您必须创建一个从 UITableViewCell 派生的新 class。完成之后,在 Storyboard 中,单击该单元格并使用 Identity Inspector 选项卡告诉它它必须使用这个新的 class 而不是默认值。
将插座连接到单元格,而不是视图。
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
}
我正在尝试设置一个表格视图,我只是 运行 遇到问题,而不是最有经验的人所以一些帮助会很好,这是情节提要 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 个错误是:
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.
在我的代码中
cell.shopLabel.text = shopData[indexPath.row].shopItemData
cell.shopButton.label = shopData[indexPath.row].shopItemPrice
线路坏了,有什么帮助吗?
您缺少一个关键概念。当您在 table 中有单元格并且要向该单元格添加自己的标签和字段时,您必须创建一个从 UITableViewCell 派生的新 class。完成之后,在 Storyboard 中,单击该单元格并使用 Identity Inspector 选项卡告诉它它必须使用这个新的 class 而不是默认值。
将插座连接到单元格,而不是视图。
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
}