Ios EXC\u BAD\u访问适用集tableHeaderView在numberOfRowsInSection中

Ios EXC\u BAD\u访问适用集tableHeaderView在numberOfRowsInSection中,ios,swift,uitableview,exc-bad-access,Ios,Swift,Uitableview,Exc Bad Access,我正试图在numberOfRowsInSection中设置tableHeaderView。但是我得到了EXC\u BAD\u ACCESS,输出控制台中没有消息 下面是我的行数部分函数 // number of rows in table view func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { //SECTION 1 if self.titles.c

我正试图在
numberOfRowsInSection
中设置
tableHeaderView
。但是我得到了
EXC\u BAD\u ACCESS
,输出控制台中没有消息

下面是我的
行数部分
函数

// number of rows in table view
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //SECTION 1
    if self.titles.count == 0 {
        self.noBookmarkView.alpha = 1.0
    }
    else {
        self.noBookmarkView.alpha = 0.0
    }
    //=====================================

    //SECTION 2
    if self.idsb.count != self.ids.count {
        self.bookmarkTableView.tableHeaderView = self.filteredView
    }
    else {
        self.bookmarkTableView.tableHeaderView = nil
    }
    //=====================================

    return self.titles.count
}
下面是我的
viewDidLoad
初始化
filteredView

@IBOutlet weak var noBookmarkView: UIView!
var filteredView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    filteredView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: bookmarkTableView.frame.width, height: 25))
    filteredView.backgroundColor = UIColor.init(red: 0.4, green: 0.4, blue: 0.71, alpha: 1.0)
    let label: UILabel = UILabel.init(frame: filteredView.frame)
    label.text = "Filtered"
    label.font = UIFont.init(name: "System", size: 14.0)
    label.adjustsFontSizeToFitWidth = true
    filteredView.addSubview(label)
}
因此,在我添加
filteredView
之前,它与
noBookmarkView
完美配合

现在它在self.noBookmarkView.alpha=0.0上出现错误
EXC\u BAD\u ACCESS
。如果我注释掉
第2节
,它将不会出错。如果我注释掉
第1节
,那么它就有
EXC\u BAD\u访问权限
在线
self.bookmarkTableView.tableHeaderView=nil

我不明白为什么它会在
self.noBookmarkView.alpha=0.0
上失败,而第2节似乎是导致问题的原因


如何解决此问题?

您应该通过
UITableView
数据源提供标题:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return self.filteredView
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 25
}

我看不出问题所在,但我想知道被声明为弱是否是问题所在。还有什么会保存对该视图的引用,并且可能会被释放和归零?@GaryMakin I将其更改为
@IBOutlet var noBookmarkView:UIView它仍然有同样的问题。这是我唯一的猜测,对不起。@GaryMakin一切都好。谢谢希望有人能找到答案。你试过清理和重建吗?有时候只需要清洁,一切都很好!成功了。我注释掉了第2节并添加了代码。如果
self.idsb.count!=self.ids.count
?您可以在
查看标题部分中进行检查,如果不想显示标题,则返回nil。您可能需要在
heightforheaderin部分中返回height=0,以达到完美!非常感谢你!!