如何在自定义视图中嵌入UITableView [英] How to embed a UITableView in a custom view

查看:72
本文介绍了如何在自定义视图中嵌入UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个具有UITableView作为子视图的自定义视图。

I want to create a custom view that has a UITableView as a subview.

自定义视图以编程方式创建表视图。但是,对于外部世界(即ViewController),自定义视图本身似乎是一个表视图。

The custom view creates the table view programmatically. To the outside world (i.e., the ViewController), though, the custom view itself would appear to be a table view.

import UIKit
class CustomTableView: UIView {
    
    // Do I make outlets?
    //@IBOutlet var dataSource: UITableViewDataSource?
    //@IBOutlet var delegate: UITableViewDelegate?
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(frame: CGRect){
        super.init(frame: frame)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        var tableView: UITableView!
        tableView = UITableView(frame: self.bounds)
        // I'm not sure how to set the delegate and dataSource
        // tableView.dataSource = ???
        // tableView.delegate = ???
        
        self.addSubview(tableView)
       
    }
}

以编程方式创建UITableView并将其作为子视图添加到自定义视图父级后,我不知道如何使自定义视图像表视图一样工作。也就是说,我不知道如何为 delegate dataSource获取自定义视图以在View Controller和表视图之间进行通信。

After creating the UITableView programmatically and adding it as a subview to the custom view parent, I can't figure out how to get the custom view to act like it is the table view. That is, I don't know how to get the custom view to do the communication between the View Controller and the table view for the delegate and dataSource.

  • Designing Your Data Source and Delegate
  • Create a static UITableView without Storyboards

这些文章看起来不错,但是我有点迷茫。

These articles seemed good, but I got a little lost.

如何使自定义视图的行为像自己的表子视图是否涉及委托和数据源?

How do I make the custom view act like it's own table subview with regard to delegate and data source?

推荐答案

解决方案是使 tableView 自定义类的属性(如@BaseZen建议)。然后在自定义类中提供属性和方法,以模仿并传递 tableView 所需的属性和方法。

The solution is to make the tableView a property of the custom class (as @BaseZen suggested). Then provide properties and methods in the custom class to mimic and pass along the properties and methods needed from tableView.

import UIKit
@IBDesignable class UICustomTableView: UIView {

    private var myTableView: UITableView

    required init(coder aDecoder: NSCoder) {
        myTableView = UITableView()
        super.init(coder: aDecoder)
    }
    override init(frame: CGRect){
        myTableView = UITableView()
        super.init(frame: frame)
    }
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    // TODO: @IBOutlet still can't be set in IB
    @IBOutlet weak var delegate: UITableViewDelegate? {
        get {
            return myTableView.delegate
        }
        set {
            myTableView.delegate = newValue
        }
    }

    // TODO: @IBOutlet still can't be set in IB
    @IBOutlet weak var dataSource: UITableViewDataSource? {
        get {
            return myTableView.dataSource
        }
        set {
            myTableView.dataSource = newValue
        }
    }

    func registerClass(cellClass: AnyClass?, forCellReuseIdentifier identifier: String) {
        myTableView.registerClass(cellClass, forCellReuseIdentifier: identifier)
    }

    func dequeueReusableCellWithIdentifier(identifier: String) -> UITableViewCell? {
        return myTableView.dequeueReusableCellWithIdentifier(identifier)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        // ...

        // setup UITableView
        myTableView.frame = self.bounds
        self.addSubview(myTableView)

        // ...
    }       
}

然后可以像普通的 UITableView

import UIKit
class TableViewDemoVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var customTableView: UICustomTableView!

    var items: [String] = ["One", "Two", "Three"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // setup the table view from the IB reference
        customTableView.delegate = self
        customTableView.dataSource = self
        customTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell: UITableViewCell = self.customTableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!
        cell.textLabel?.text = self.items[indexPath.row]            
        return cell
    }

    // ...
}

这篇关于如何在自定义视图中嵌入UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆