swift - 快速访问表格 View 中的每个标题和控件

标签 swift uitableview header nstableheaderview

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

提前致谢

最佳答案

为了从header获取buttonimage,您可以为实现自定义的UIView >标题,例如

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个自定义标题 View 。

func getHeaders() {

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

关于swift - 快速访问表格 View 中的每个标题和控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33007142/

相关文章:

Android:Android 相机预览上的叠加层

c - 在头文件中声明全局变量 `extern const int` 但在源文件中仅声明 `int`

ios - 使用 Xib 时 UITableView 和 UICollectionView 的实现有什么区别?

iOS:使导航栏在 UITableViewController 中始终可见

swift - 我应该如何命名返回 "continue"标志的委托(delegate)方法?

ios - Twitter iOS 配置文件页面配置

ios - 更新约束不适用于 UITableView snapkit

python - 为什么更新此查询时页面响应没有改变?

ios - Alamofire 上传巨大的文件

ios - swift : How to get the string before a certain character?