gpt4 book ai didi

ios - 在 Swift 中使用另一个文件中的单元格

转载 作者:行者123 更新时间:2023-11-30 14:18:46 32 4
gpt4 key购买 nike

我想将 UITableView 单元格的内容保留在一个单独的文件中,就像在 Objective-c 中所做的那样,甚至在我的 TodayExtension Swift 表中,以便将其连接到 Storyboard。然而,当我尝试这样做时,它提示找不到单元格的类别;这是我使用的功能:

override func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
println(indexPath)
let cell = tableView.dequeueReusableCellWithIdentifier(
TableViewConstants.cellIdentifier,
forIndexPath: indexPath) as! TodayCell
let entry = busCollection[indexPath.row]
cell.bus=entry.bus
cell.destination=entry.destination;
return cell

}

tod​​ayCell 是另一个文件中的类,它报告找不到:

import UIKit
class TodayCell: UITableViewCell {
@IBOutlet var bus: UILabel!
@IBOutlet var stopAddress: UILabel!
@IBOutlet var destination: UILabel!
}

即使根据 Swift 文档不需要,导入文件也会将错误移至导入语句中。

最佳答案

以编程方式:

override func viewDidLoad() {
super.viewDidLoad()

tableView.registerClass(todayCell.self, forCellReuseIdentifier: "todayCell")
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("todayCell", forIndexPath: indexPath) as! todayCell

// Setup

return cell
}

这应该可以。

Storyboard:
如果您使用原型(prototype),则只需将原型(prototype)类设置为 todayCell 和一个标识符。当您在 tableView.dequeueReusableCellWithIdentifier()

中创建单元格时,将使用此标识符

identifier

class

关于ios - 在 Swift 中使用另一个文件中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30777091/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com