UITableView的交互式部分头
我有UITableView
我已经添加了一个UIButton
。 问题是它只显示在标题的顶部。 如何在所有节标题中添加button,以便使节可以交互?
这里是在UITableView
Section Header上添加一个button的代码
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) }
任何人都可以告诉我如何在每个部分添加交互式button?
你必须使用这个委托
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 }
你可以使用这个委托方法Tableview:
optional public func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?