Byt*_*yte 8 uitableview custom-cell ios swift uitableviewsectionheader
我使用Xib文件而不是故事板.我创建了一个包含一些虚拟数据的表视图.它的工作正常,现在我已经创建了一个带有一些标签和textFields的自定义单元格.如何将其用作UITableView的页眉或页脚?
Tus*_*rde 29
答案其实很简单.在viewForHeaderInSection()创建单元格对象时,cellForRowAtIndexPath()并返回它.
这是示例代码.
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") as! YourTableViewCell
return cell
}
Run Code Online (Sandbox Code Playgroud)
您可以将标题的高度设置为:
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 80
}
Run Code Online (Sandbox Code Playgroud)
更新Swift 4.2
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier") as! YourTableViewCell
return cell
}
Run Code Online (Sandbox Code Playgroud)
和
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 80
}
Run Code Online (Sandbox Code Playgroud)
您可以通过编程方式创建自定义视图。我尝试了以下代码。尝试一次。
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let customview:UIView = UIView();
let label = UILabel();
label.text = "ur custom data"
/*
You can add any number of subviews you want here
And don't forget to add it to your customview.
*/
customview.addSubview(label)
return customview;
}
Run Code Online (Sandbox Code Playgroud)
同样,您也可以对标头执行相同的操作。
| 归档时间: |
|
| 查看次数: |
13293 次 |
| 最近记录: |