sách gpt4 ai đã đi

Tại sao chúng ta không làm theo cùng một quy trình để tạo customTableViewCell bằng xib như khi tạo customView bằng xib?

In lại 作者:行者123 更新时间:2023-11-28 10:10:42 31 4
mua khóa gpt4 Nike

当我们创建自定义 View 时,我们将 View 文件的所有者设置为自定义类,并使用 initWithFrame 或 initWithCode 对其进行实例化。

当我们创建 customUITableViewCell 时,我们将 View 的类设置为自定义类,而不是文件所有者的类。然后注册所有的 Nib 等等。通过这种方式,我们总是需要将 xibs 注册到 UIViewController 和

let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)等等。

我发现我不想一直在我想使用 customUITableViewCell 的地方注册 nib。所以我想在我的 customUITableCell 中初始化 xib,就像创建 customUIView 的过程一样。我成功了。这是步骤。

我的问题是创建 customUITableCell 的首选方式是什么?使用此方法,无需注册 nib,我们可以在需要的地方调用 customCell,而无需加载/注册 nib。

  1. 将 xib 的 View 文件所有者设置为 customUITableCell 类。不是 View 的类设置为 customClass,只是文件的所有者。

Image 1

  1. 我的自定义类名为 myView:UITableViewCell

    nhập khẩu 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)

    }
    }
  2. 在 UIVivController 中,我没有注册和使用 nib

let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

相反,我这样做了。

让 cell = myView(style: .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
}
}
  1. 这是结果。

Image 4

感谢您的宝贵时间!!!

1 Câu trả lời

您的方法意味着每次 UITableView 请求一个新单元格时,您都是从头开始创建一个全新的单元格。这意味着它必须:

  1. 找到 Nib
  2. 加载 Nib
  3. 解析它以找到 View
  4. 发表意见
  5. 更新单元格

这并不比使用自定义 View 的长 ScrollView 更好。

UITableView 的美妙之处在于它优化了大部分过程并重新使用单元格,从而大大降低了单元格多于屏幕显示的性能成本。 使用传统(正确)方法,步骤 1-4 只需执行一次.

扩展 xib 中的差异:

当使用 UITableView 创建单元格时,您只需要给它 nib,系统就会在 nib 中查找 UITableViewCell。一个简单的 UIView 将不起作用。

您实际上Có thểTrong của bạnxib 中使用您的自定义类对UIView 进行子类化。恰好规范是使用 fileOwner,这主要是因为这是在前 Storyboard时代要求将 nib 与 UIViewControllers 一起使用时的规范

关于ios - 为什么我们不遵循使用 xib 创建 customTableViewCell 的相同过程,就像使用 xib 创建 customView 一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48590736/

31 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com