我有一个带有自定义标题的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
获取button
和image
,您可以为实现自定义的
,例如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/