ios UITableView - 使用 Swift 注册类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25347406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:51:52  来源:igfitidea点击:

UITableView - registerClass with Swift

iosswiftuitableviewregisterclass

提问by Stuart Breckenridge

Is anyone else having an issue using the tableView.registerClassmethod with Swift?

还有其他人在使用tableView.registerClassSwift的方法时遇到问题吗?

It no longer comes in code completion for me (nor can I use it if manually typed) but it is still in the headers...

它不再出现在我的代码完成中(如果手动输入,我也不能使用它)但它仍然在标题中......

Code Completion

代码完成

UITableView headers

UITableView 标题

回答by Woodstock

It works for me perfectly.

它非常适合我。

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

Exactly as I have it above.

正如我上面所说的那样。

回答by Dinesh Katwal

For Swift 2.2 Register For Default Cell From Class

对于 Swift 2.2 从类注册默认单元格

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")



For Swift 3.0 Register For Default Cell From Class



对于 Swift 3.0 从类注册默认单元格

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")



For Swift 2.2 Register For Default Cell From Nib



对于 Swift 2.2 从 Nib 注册默认单元格

self.tableView.registerNib(UINib(nibName: "CustomCellName", bundle: nil), forCellReuseIdentifier: "CustomCellIdentifer")



For Swift 3.0 Register For Default Cell From Nib



对于 Swift 3.0 从 Nib 注册默认单元格

self.tableView.registerNib(UINib(nibName: "CustomCellName", bundle: nil), forCellReuseIdentifier: "CustomCellName")

Note:Storyboard created cell is called prototype cell and have some procedure to register prototype cell like Nib.And Don't forget to set the cell identifier like below. enter image description here

注意:Storyboard 创建的单元格称为原型单元格,并且有一些程序可以像 Nib 一样注册原型单元格。不要忘记设置单元格标识符,如下所示。 在此处输入图片说明

回答by Qiulang

Swift has once again renamed it to

Swift 再次将其重命名为

tableView.register(UITableViewCell.self, forCellReuseIdentifier:"DefaultCell")

表视图。注册(UITableViewCell.self, forCellReuseIdentifier:"DefaultCell")

Really don't understand why they bothered so much about this particular naming

真的不明白为什么他们对这个特定的命名如此费心

回答by swiftBoy

Updated for Swift 5

为 Swift 5 更新

Register TableView Cell in viewDidLoad If you are using Default Cell

如果您使用的是默认单元格,则在 viewDidLoad 中注册 TableView 单元

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyTableCell")

Register TableView Cell in viewDidLoad If you are using Custom Nib/XIB Cell

在 viewDidLoad 中注册 TableView Cell 如果您使用的是Custom Nib/XIB Cell

tableView.register(UINib(nibName: "MyCustomCell", bundle: nil), forCellReuseIdentifier: "MyCustomCell")

回答by Rinju Jain

For swift 4

快速 4

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")

回答by Gurjinder Singh

Swift 4 and 4.1. making generic methods it is very easy to register and dequeue table cell.

斯威夫特 4 和 4.1。制作通用方法很容易注册和出列表格单元格。

       override func viewDidLoad() {
                super.viewDidLoad()
                self.tblView.register(CellProfileOther.self) // cell class name
       }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                let cell: CellProfileOther = tableView.dequeueReusableCell(forIndexPath: indexPath)
                return cell
            }
        extension UITableView {
        func register<T:UITableViewCell>(_: T.Type) where T: ReusableView, T: NibLoadableView {
                let bundle = Bundle(for: T.self)
                let nib = UINib(nibName: T.nibName, bundle: bundle)
                self.register(nib, forCellReuseIdentifier: T.defaultReuseIdentifier)
            }
        func dequeueReusableCell<T:UITableViewCell>(forIndexPath indexPath: IndexPath) -> T where T: ReusableView {
                guard let cell = self.dequeueReusableCell(withIdentifier: T.defaultReuseIdentifier, for: indexPath) as? T else {
                    fatalError("Could not dequeue cell with identifier: \(T.defaultReuseIdentifier)")
                }
                return cell
            }
        }

        protocol ReusableView: class {
            static var defaultReuseIdentifier: String { get }
        }

        protocol NibLoadableView: class {
            static var nibName: String { get }
        }

        extension ReusableView where Self: UIView {
            static var defaultReuseIdentifier: String {
                return String(describing: Self.self)
            }
        }    
        extension NibLoadableView where Self: UIView {
            static var nibName: String {
                return String(describing: Self.self)
            }
        }

   // Here is cell class 
    class CellProfileOther: UITableViewCell, ReusableView, NibLoadableView  {
    }

回答by Zgpeace

I prefer to use TableViewCell.self to generate the identifier string. It can reduce the typing error.

我更喜欢使用 TableViewCell.self 来生成标识符字符串。它可以减少打字错误。

tableView.register(MyCell.self, forCellReuseIdentifier: String(describing: MyCell.self))

回答by Amit Thakur

For swift 3 refer this. It works!

对于swift 3,请参阅this。有用!

Inside you viewdidload function

在你里面 viewdidload 函数

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "LabelCell")

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "LabelCell")

回答by Henry Heleine

Switching the order in which I called registerNib and registerClass worked for me!

切换我调用 registerNib 和 registerClass 的顺序对我有用!

For some reason my app crashed when I had:

出于某种原因,我的应用程序在我遇到以下情况时崩溃了:

...registerNib.....

...注册笔尖.....

...registerClass...

...注册类...

But ran fine when I had:

但是当我有:

...registerClass...

...注册类...

...registerNib.....

...注册笔尖.....

Hope that helps some of you.

希望对你们中的一些人有所帮助。