【发布时间】:2016-08-13 13:42:17
【问题描述】:
我正在尝试设置一个表格视图,但我一直遇到问题,而不是最有经验的人,所以一些帮助会很棒,故事板如下 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”类型的值没有成员“shopLabel”
'UITableViewCell' 类型的值没有成员 'shopButton'
从 upgradesTest 到 UILabel 的 shopLabel 出口无效。 插座不能连接到重复的内容。商店按钮 从 upgradesTest 到 UILabel 的出口无效。奥特莱斯不能 连接到重复的内容。
在代码中我的
cell.shopLabel.text = shopData[indexPath.row].shopItemData
cell.shopButton.label = shopData[indexPath.row].shopItemPrice
线路不工作,有什么帮助吗?
【问题讨论】:
-
您的网点(shopButton 和 shopLabel)不在您的单元格上,它们在您的 UIViewController 子类上。如果您打算添加这样的子视图,则需要一个自定义单元子类。就目前而言,您将这些属性放在错误的位置。
-
修复了网点,现在在名为“Shop”的自定义单元格中更改 shopLabel 上的文本的正确代码行是什么
-
您的自定义单元格需要注册一个重用标识符,并且您需要将 dequeueReusableCellWithIdentifier() 的返回值转换为 cellForRowAtIndexPath 中的适当类。如果没有这些步骤,您仍然在对不具有这些属性的 UITableViewCell 实例进行操作。在从这一点开始之前,我肯定会退后一步阅读继承。
标签: ios swift uitableview iboutlet