快速访问tableview中的每个标题和控件 [英] Access each header and controls in the tableview in swift

查看:56
本文介绍了快速访问tableview中的每个标题和控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UITableView with customized header. In that header I have a button with image.

 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
}

我有五个头段.

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

in separate function how can get the image of the each button, need to access each button

例如:

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
}

预先感谢

推荐答案

为了从 header 中获取 button image ,您可以为 header 实现自定义的 UIView ,例如

In order to get button and image from header, you can implement a customized UIView for header, like

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

    func init() {
        ...
    }
} 

并在 tableView(tableView:UITableView,viewForHeaderInSection部分:Int)回调中返回此 HeaderView .

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

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

Then use tableView.headerViewForSection(section:) to get 5 customized headers view.

func getHeaders() {

    for index in 1...5 {
         let header = tableView.headerViewForSection(section: index) as! HeaderView
         let button = header.button
         let image = header.image
    }
}

这篇关于快速访问tableview中的每个标题和控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆