ios - 如何快速使用自定义 View ?

标签 ios swift uiview uikit

我想在 TableViewHeader 中添加自定义 View 。但是,当我运行以下代码时,它会创建一个 Cycle 并卡住任何用户交互的应用程序。

import UIKit
class ExpandableView: UIView {

    @IBOutlet weak var userImgView: UIImageView!
    @IBOutlet weak var userNamelbl: UILabel!
    @IBOutlet weak var countLbl: UILabel!
    @IBOutlet weak var rightArrowImgView: UIImageView!
    var isExpanded = false
    var contentView: UIView!
    @IBOutlet weak var checkMarkView: UIImageView!

    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    public override init(frame: CGRect) {
        super.init(frame: frame)
        setUpView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUpView()
    }

    private func setUpView() {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: "ExpandableView", bundle: bundle)
        self.contentView = nib.instantiate(withOwner: self, options: nil).first as? UIView
        addSubview(contentView)
        contentView.center = self.center
        contentView.autoresizingMask = []
        contentView.translatesAutoresizingMaskIntoConstraints = true
    }
}

我的使用方式如下:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let frame = CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 60)
    let expandabelView = ExpandableView(frame: frame)
    return expandabelView
}

运行时显示以下错误。

See Errors

最佳答案

可能还有很多其他方法,但我建议您使 ExpandableView 可重用以提高性能。

<小时/>

首先,简化您的 ExpandableView 类:

import UIKit

class ExpandableView: UITableViewHeaderFooterView {
    @IBOutlet weak var userImgView: UIImageView!
    @IBOutlet weak var userNamelbl: UILabel!
    @IBOutlet weak var countLbl: UILabel!
    @IBOutlet weak var rightArrowImgView: UIImageView!
    var isExpanded = false

    @IBOutlet weak var checkMarkView: UIImageView!
}

请不要错过父类(super class)是 UITableViewHeaderFooterView,而不是 UIView

第二,检查 ExpandableView.xib 的设置:

定义 View 的自定义 View 设置需要为ExpandableView

Custom View of the view

当您无法从下拉列表中选择ExpandableView时,您可能需要手动输入。不要忘记选中从目标继承模块

文件所有者的自定义 View 设置需要为空。

File's Owner

如果已经设置了某个类,请手动将其删除。

确认所有 socket 均已正确连接到您的ExpandableView

Outlets

(修改 xib 后,最好重新连接它们。)

您可能需要重新构建 View 层次结构和/或约束

第三,修改持有表格 View 的 View Controller 。

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        tableView.delegate = self
        tableView.dataSource = self
        //...
        let nib = UINib(nibName: "ExpandableView", bundle: nil)
        tableView.register(nib, forHeaderFooterViewReuseIdentifier: "ExpandableView")
        tableView.estimatedSectionHeaderHeight = 60
        tableView.sectionHeaderHeight = UITableView.automaticDimension
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let expandabelView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "ExpandableView")
        // Frame size should be represented with constraints.
        return expandabelView
    }

关于ios - 如何快速使用自定义 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54180547/

相关文章:

ios - 这是什么ViewController

swift - Firebase 查询排序无法正常工作

ios - 查看动画后发生的转换

iOS - UIScrollView 从半屏到全屏

ios - Swift iOS - TextField 初始宽度错误的底部边框

ios - Swift:如何将经常使用的代码放在一个地方以便重用?

ios - Swift:我必须调用 Facebook 登录按钮两次才能获取 FBSDKAccessToken

ios - 使用 chrome cast 我可以直接从我的 iPhone 流式传输吗?

ios - 使用 Swift 更改 UISlider 图像

ios - 为什么我的 Sprite 节点没有在我的通用应用程序上调整大小?