首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用在情节提要中定义的自定义类

使用在情节提要中定义的自定义类
EN

Stack Overflow用户
提问于 2019-06-02 16:58:25
回答 4查看 129关注 0票数 1

在Xcode中,我可以在标识符检查器中定义一个自定义类,但是如何使用它们呢?下面是示例:

代码语言:javascript
运行
复制
class c1 : UITableViewCell {

  func celltest() {
        let i = 99;
    }
}

class NicePlaceTableViewController: UITableViewController {
.
.
.
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath)
        **cell.celltest()** .. has no member celltest
        let place = places[indexPath.row]
        cell.textLabel?.text = place.name
        cell.detailTextLabel?.text = "\(place.timestamp)"
        return cell
    }

如果reuseIdentifier是已知的,但不知道它的自定义类--在本例中是" c1“--我如何才能在不违反Xcode编译检查的情况下访问类c1中定义的方法?

EN

Stack Overflow用户

发布于 2019-06-02 18:06:52

Xcode Interface Builder是一个可视化创建UI扫描的IDE。

您对表视图中的单元格的配置将使用在表视图中注册的c1单元格实例化一个NicePlaceTableViewController。

如果没有使用Interface Builder工具的Xcode,则必须自己编程创建它,如下所示:

代码语言:javascript
运行
复制
class NicePlaceTableViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(c1.self, forCellReuseIdentifier: "Cell1")
    }
}

这段代码为给定的标识符注册一个类类型。要访问单元,您必须使用tableView.dequeueReusableCell方法,该方法将作用于内部池来创建或重用单元。

代码语言:javascript
运行
复制
 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath)
}

出列的信元是UITableViewCell类型。如果你想将它转换为c1类型,那么你必须使用!操作符。

代码语言:javascript
运行
复制
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath) as! c1
票数 1
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56413602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档