2

我有一个UITableView自定义标题。在该标题中,我有一个带有图像的按钮。

 func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let cView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))

    itemNumBtn = UIButton(frame: CGRectMake(0, 0, 70, 42))
    itemNumBtn.setBackgroundImage(UIImage(named: "hide.png"), forState: .Normal)
    cView.addSubview(itemNumBtn)

    return cView
}

我有五个标题部分。

在单独的函数中如何获取每个按钮的图像,需要访问每个按钮

前任:

fun getHeader() {
    // how to access each header and buttons
    var eachBtn = each button in the header
    var image = all the images of all header section
}

提前致谢

4

1 回答 1

4

为了获取buttonimage来自header,您可以实现一个自定义UIView的 for header,例如

class HeaderView: UIView {
    var button: UIButton
    var image: UIImage

    func init() {
        ...
    }
} 

HeaderView在您的tableView(tableView: UITableView, viewForHeaderInSection section: Int)回调中返回它。

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return HeaderView()
}

然后用于tableView.headerViewForSection(section:)获取 5 个自定义标题视图。

func getHeaders() {

    for index in 1...5 {
         let header = tableView.headerViewForSection(section: index) as! HeaderView
         let button = header.button
         let image = header.image
    }
}
于 2015-10-08T05:41:05.953 回答