gpt4 book ai didi

ios - UITableView 的交互部分标题

转载 作者:可可西里 更新时间:2023-11-01 01:37:57 25 4
gpt4 key购买 nike

我有 UITableView,我在其中添加了一个 UIButton。问题是它只显示在标题的顶部。如何在所有部分标题中添加按钮,以便使该部分具有交互性?

这是在 UITableView 的 Section Header 上添加按钮的代码

    func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
header.contentView.backgroundColor = UIColor(red: 255/255, green: 200/255, blue: 20/255, alpha: 1.0) // Just some random color
header.textLabel!.textColor = UIColor.whiteColor()
let btn = UIButton(type: UIButtonType.Custom) as UIButton
btn.frame = CGRectMake(header.frame.width - 62, 0, 50, 50)
btn.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
btn.setTitle("Direction", forState: .Normal)
btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
header.contentView.addSubview(btn)
}

谁能告诉我如何在每个部分添加交互按钮?

最佳答案

你必须使用这个委托(delegate)

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

自定义tableview的headerview部分

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let header: = UIView()
let btn = UIButton(type: UIButtonType.Custom) as UIButton
btn.frame = CGRectMake(header.frame.width - 62, 0, 50, 50)
btn.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
btn.setTitle("Direction", forState: .Normal)
btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)

header.addSubview(btn)
return header

}

关于ios - UITableView 的交互部分标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33538350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com