【问题标题】:Access each header and controls in the tableview in swift快速访问表格视图中的每个标题和控件
【发布时间】:2016-01-05 13:33:52
【问题描述】:

我有一个带有自定义标题的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
}

提前致谢

【问题讨论】:

    标签: swift uitableview header nstableheaderview


    【解决方案1】:

    为了从header得到buttonimage,可以为header实现一个自定义的UIView,like

    class HeaderView: UIView {
        var button: UIButton
        var image: UIImage
    
        func init() {
            ...
        }
    } 
    

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

    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
        }
    }
    

    【讨论】:

    • 感谢它的工作,但我使用 UITableViewHeaderFooterView 而不是子类视图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多