简体   繁体   English

ios如何快速与自定义单元中的插座进行交互

[英]ios swift how to interact with outlets in custom cell

nomrally i have one view controller and i add outlets to it. 通常,我有一个视图控制器,并向其中添加了插座。 and inside view did load i can change the font of the text view or i can add images to the ui image. 并且内部视图确实加载了,我可以更改文本视图的字体,也可以将图像添加到ui图像。 but now i have a custom cell like this: 但现在我有一个像这样的自定义单元:

class NumberOfPeopleTableViewCell: UITableViewCell {

    @IBOutlet weak var numberOfPeopleLabel: UILabel!

as you see, i have outlet in that cell, how can i interact with that outlet? 如您所见,我在那个单元中有插座,我该如何与该插座交互? there is no view did load function inside the table view cell 表格视图单元内没有视图执行加载功能

You could set the font of the label in your storyboard. 您可以在情节提要中设置标签的字体。

Or you could set the font of the label in awakeFromNib (a method you can override in your cell class). 或者,您可以在awakeFromNib设置标签的字体(可以在单元格类中覆盖的方法)。

Or you could set the font of the label in tableView(_:cellForRowAtIndexPath:) (in your table view controller). 或者,您可以在tableView(_:cellForRowAtIndexPath:) (在表格视图控制器中)设置标签的字体。

You can override the function layoutSubviews 您可以覆盖函数layoutSubviews

override func layoutSubviews() {
    //do whatever here for your outlets        
}

From what I can gather, the "standard" approach would be, inside your UITableViewController subclass: 据我所知,“标准”方法是在UITableViewController子类内部:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("<Your Id Here>", forIndexPath: indexPath) as! NumberOfPeopleTableViewCell
        let foo = cell.numberOfPeopleLabel // DO STUFF WITH IT
        // Do more stuff here
        return cell
}

OR -- 要么 -

If you want to keep your View Controller code clean of graphics layout-ing / appearance logic (which you should) consider, inside NumberOfPeopleTableViewCell to override func layoutSubviews() . 如果您想让View Controller代码保持图形布局/外观逻辑的清洁(应该考虑),请在NumberOfPeopleTableViewCell内重写func layoutSubviews()

OR (my favourite) -- 或(我的最爱)-

You could subclass the UITextView (eg MYCellTextView ) and change the appearance for all of your subclass instances in your app delegate using UIApparence. 您可以使用MYCellTextView子类化UITextView(例如MYCellTextView ),并更改应用程序委托中所有子类实例的外观。 Like this should work: 这样应该可以工作:

MyCellTextView.appearance().font = UIFont(name: "<myFont>", size: 10.0)

You can put the above, for example, in your app delegate. 例如,您可以将以上内容放入您的应用程序委托中。 This has the advantage of keeping appearance code in one place and away from view controllers (and generic, for all instances of your custom view). 这具有将外观代码保持在一个地方并且远离视图控制器(对于您的自定义视图的所有实例都是通用的)的优点。

take a look at awakeFromNib . 看看awakeFromNib as the documentation states: 文档所述:

When an object receives an awakeFromNib message, it is guaranteed to have all its outlet instance variables set. 当对象收到awakeFromNib消息时,可以确保已设置其所有出口实例变量。

so you can do: 因此,您可以执行以下操作:

override func awakeFromNib() {
  super.awakeFromNib()

  numberOfPeopleLabel.font = UIFont.systemFontOfSize(15.0) // or whatever font you want to assign
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM