【问题标题】:Outlets and UITableView troubleOutlets和UITableView的麻烦
【发布时间】: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


【解决方案1】:

您缺少一个关键概念。当您在表格中有单元格并且您正在向该单元格添加自己的标签和字段时,您必须创建一个派生自 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
}

【讨论】:

  • 成功了!谢谢 c:,对于任何使用此资源的人来说,它的 shopButton.setTitle 不是 shopButton.label
  • 我认为 shopButton.title = "some string" 也可以。试试看。
猜你喜欢
  • 2011-02-11
  • 2015-04-12
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多