当我们创建customView时,我们将视图文件的所有者设置为自定义类,并使用initWithFrame或initWithCode实例化它。
当我们创建customUITableViewCell时,我们将视图的类设置为自定义类,而不是File的所有者。然后注册所有的笔尖。 这样,我们总是需要将xibs注册到UIViewController和
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
等等。
我发现我不想在我想要使用customUITableViewCell的时候注册nib。所以我想在我的customUITableCell中初始化xib,就像创建customUIView的过程一样。我成功了这是步骤。
我的问题是创建customUITableCell的首选方式是什么? 使用此方法无需注册nib,我们可以在不加载/注册nib的情况下调用customCell。
我的自定义类名为myView:UITableViewCell
import UIKit
class myView: UITableViewCell {
var subView: UIView!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initSubviews()
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
initSubviews()
}
func initSubviews(){
subView = Bundle.main.loadNibNamed("TableViewCell", owner: self, options: nil)?.first as! UIView
subView.autoresizingMask = UIViewAutoresizing(rawValue: UIViewAutoresizing.RawValue(UInt8(UIViewAutoresizing.flexibleWidth.rawValue) | UInt8(UIViewAutoresizing.flexibleHeight.rawValue)))
self.addSubview(subView)
}
}
İnsideUIVivController,我没有注册nib并使用
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
相反,我这样做了。
让cell = myView(样式:.default,reuseIdentifier:“TableViewCell”)
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var tableStyle: UITableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
tableStyle.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
tableStyle.delegate = self
tableStyle.dataSource = self
view.addSubview(tableStyle)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100.00
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myView(style: .default , reuseIdentifier: "TableViewCell")
return cell
}
}
感谢您的时间!!!
答案 0 :(得分:4)
您的方法意味着每次UITableView
请求新单元格时,您都是从头开始创建一个全新的单元格。这意味着它必须:
这并不比拥有自定义视图的长滚动视图更好。
UITableView
的美妙之处在于它优化了这个过程的大部分并重新使用了细胞,大大降低了拥有更多细胞而不是适合屏幕的性能成本。 使用传统(正确)方法,步骤1-4只需要发生一次。
扩展xib中的差异:
使用UITableView
创建单元格时,只需为其指定nib,系统会在nib中查找UITableViewCell
。一个简单的UIView
将不起作用。
您实际上可以使用您的自定义类继承UIView
中的xib
。恰好是规范是使用fileOwner
,主要是因为在故事板前时代需要使用UIViewControllers
的笔尖时这是常态
答案 1 :(得分:0)
对接受的答案的补充:
如果您使用“经典”方法的唯一问题是您需要注册笔尖并调用dequeueReusableCell
,您可以使用this article中讨论的协议扩展来简化调用:
protocol ReuseIdentifying {
static var reuseIdentifier: String { get }
}
extension ReuseIdentifying {
static var reuseIdentifier: String {
return String(describing: Self.self)
}
}
extension UITableViewCell: ReuseIdentifying {}
要注册,请致电
self.tableView.register(UINib(nibName: MyTableViewCell.reuseIdentifier, bundle: nil), forCellReuseIdentifier: MyTableViewCell. reuseIdentifier)
要创建它,请致电
let cell = self.tableView.dequeueReusableCell(withIdentifier: MyTableViewCell. reuseIdentifier, for: indexPath) as! MyTableViewCell
(当然,这只有在类,xib和重用标识符都具有相同名称时才有效)