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

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中定义的方法?
发布于 2019-06-02 17:03:07
您只需将其强制转换为c1即可
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath) as! c1
cell.celltest()顺便说一句,c1不是一个好的类名。
发布于 2019-06-02 17:12:08
您只需要将该单元格类型转换为c1的对象。您可以通过在cellForRowAt方法中编写以下代码来完成此操作:
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath) as! c1因此,编译器将知道您的自定义表视图单元格类的类型,并且cell.celltest()不会给您一个错误。
swift样式指南的另一个参考,它可以在类名和方法名方面对你有很大帮助:
发布于 2019-06-02 17:26:43
我不知道类名"c1“。有没有动态获取它的方法?我不明白为什么通过单元格的reuseIdentifier "cell1“查询单元格,并且xcode不知道应该指定单元格的类是什么?(c1)
https://stackoverflow.com/questions/56413602
复制相似问题