比较来自世界各地的卖家的域名和 IT 服务价格

swift - 如何创建自定义 viewForHeaderInSection, 使用文件 XIB?

我可以创建一个简单的用户 viewForHeaderInSection 软件,如下所示。 但我想制作更复杂的东西,也许是与另一个阶级的联系,实现他们的属性,例如细胞 tableView. 我只是想看看我做了什么。


func tableView/tableView: UITableView, viewForHeaderInSection section: Int/ -> UIView? {

if/section == 0/ {

let view = UIView// // The width will be the same as the cell, and the height should be set in tableView:heightForRowAtIndexPath:
let label = UILabel//
let button = UIButton/type: UIButtonType.System/

label.text="My Details"
button.setTitle/"Test Title", forState: .Normal/
// button.addTarget/self, action: Selector/"visibleRow:"/, forControlEvents:.TouchUpInside/

view.addSubview/label/
view.addSubview/button/

label.translatesAutoresizingMaskIntoConstraints = false
button.translatesAutoresizingMaskIntoConstraints = false

let views = ["label": label, "button": button, "view": view]

let horizontallayoutContraints = NSLayoutConstraint.constraintsWithVisualFormat/"H:|-10-[label]-60-[button]-|", options: .AlignAllCenterY, metrics: nil, views: views/
view.addConstraints/horizontallayoutContraints/

let verticalLayoutContraint = NSLayoutConstraint/item: label, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0/
view.addConstraint/verticalLayoutContraint/

return view
}

return nil
}


func tableView/tableView: UITableView, heightForHeaderInSection section: Int/ -> CGFloat {
return 50
}


有没有人解释我如何创建自定义类型的标题 tableView 通过 xib? 我遇到了旧主题 Obj-C, 但我是一个纽伯语言 Swift. 如果有人解释了相同的细节,那将是很棒的。

1.issue:

按钮 @IBAction 不与我联系 ViewController.

/固定的

/

用文件的所有者解决了基本类 ViewController /按下左电路菜单。/

2.issue:

标题高度的问题

/固定的

/

解决了补充.

headerView.clipsToBounds = true

在一种方法中

viewForHeaderInSection

:.

关于限制的警告

https://coderoad.ru/11664115/
:

当我加了 ImageView 甚至与此方法相同的高度限制 viewController, 他流入了 tableView 行
https://i.stack.imgur.com/yoYQ6.png
.


func tableView/tableView: UITableView, heightForHeaderInSection section: Int/ -> CGFloat {
return 120
}


如果我用 automaticallyAdjustsScrollViewInsets 在 viewDidLoad, 然后在这种情况下,图像流下来 navigationBar. - 固定的-


self.automaticallyAdjustsScrollViewInsets = false


3.issue:

如果按钮是下面的

/固定的

/


@IBAction func didTapButton/sender: AnyObject/ {
print/"tapped"/

if let upView = sender.superview {
if let headerView = upView?.superview as? CustomHeader {
print/"in section \/headerView.sectionNumber/"/
}

}
}
已邀请:

三叔

赞同来自:

基于的标题的典型过程 NIB 将:

创建一个子类
UITableViewHeaderFooterView

至少为你的标签出口。 您可能还想给他一些可以重新投影的标识符,哪个部分对应于此标题。 类似地,您可以指定一个协议,其中标题可以通知报告控制器以进行事件。 /例如,按下按钮/. 所以在 Swift 3 然后:


// if you want your header to be able to inform view controller of key events, create protocol

protocol CustomHeaderDelegate: class {
func customHeader/_ customHeader: CustomHeader, didTapButtonInSection section: Int/
}

// define CustomHeader class with necessary `delegate`, `@IBOutlet` and `@IBAction`:

class CustomHeader: UITableViewHeaderFooterView {
static let reuseIdentifier = "CustomHeader"

weak var delegate: CustomHeaderDelegate?

@IBOutlet weak var customLabel: UILabel!

var sectionNumber: Int! // you don't have to do this, but it can be useful to have reference back to the section number so that when you tap on a button, you know which section you came from; obviously this is problematic if you insert/delete sections after the table is loaded; always reload in that case

@IBAction func didTapButton/_ sender: AnyObject/ {
delegate?.customHeader/self, didTapButtonInSection: section/
}

}


创建 NIB. 就个人而言,我给了 NIB 与基本类相同的名称,以简化我的项目中我的文件管理并避免混淆。 在任何情况下,关键步骤包括:

创建视图 NIB 或者,如果你开始空 NIB, 添加视图B. NIB.;

安装基本演示类作为子类是
UITableViewHeaderFooterView

/在我的例子中
CustomHeader

/;

添加您的控件和限制 IB;

连接
@IBOutlet

引用代码中的套接字 Swift;

连接K.按钮
@IBAction

; 和

对于root演示文稿 NIB 确保安装了背景颜色 "default", 否则,您将对改变背景颜色变得讨厌的警告。



viewDidLoad

在视图控制器中注册 NIB. 在 Swift 3 然后:


override func viewDidLoad// {
super.viewDidLoad//

tableView.register/UINib/nibName: "CustomHeader", bundle: nil/, forHeaderFooterViewReuseIdentifier: CustomHeader.reuseIdentifier/
}



viewForHeaderInSection

使用您在上一步中指定的相同标识符从队列重用视图中删除。 完成此操作中,您现在可以使用您的插座,您无需使用软件创建限制等。唯一需要做的事情 /对按钮按钮的协议/, 它由她的代表表示。 例如,在 Swift 3:


override func tableView/_ tableView: UITableView, viewForHeaderInSection section: Int/ -> UIView? {
let headerView = tableView.dequeueReusableHeaderFooterView/withIdentifier: "CustomHeader"/ as! CustomHeader

headerView.customLabel.text = content[section].name // set this however is appropriate for your app's model
headerView.sectionNumber = section
headerView.delegate = self

return headerView
}

override func tableView/_ tableView: UITableView, heightForHeaderInSection section: Int/ -> CGFloat {
return 44 // or whatever
}


显然,如果您要将视图控制器指定为
delegate

对于标题视图中的按钮,您必须遵守此协议:


extension ViewController: CustomHeaderDelegate {
func customHeader/_ customHeader: CustomHeader, didTapButtonInSection section: Int/ {
print/"did tap button", section/
}
}


当我列出所有必要的步骤时,所有这些都声音令人困惑,但实际上,如果您完成了一次或两次,则非常简单。 我认为它比以编程方式建立标题视图更容易。



抗议:

问题是你不能做出一种神奇的方式来转向
UIView

在尖端B.
UITableViewHeaderFooterView

, 只是在识别检查员那样宣布它。

这是错误的。 如果使用上述基于上述方法 NIB, 然后是类,它的实例是为标题的这个视图的根播出创建的实例,

是一个

子类
UITableViewHeaderFooterView

, 但不是
UIView

. 它创建了一个类别,用于为根表示为基类指定的任何类。 NIBs.

但是,正确的事实是这个课程的一些属性 /特别的
https://developer.apple.com/do ... tview
/ 没有基于这种方法使用的 NIB. 它真的应该是一个可选的财产
https://developer.apple.com/do ... label
textLabel


https://developer.apple.com/do ... label
/或者,更好,他们必须增加适当的支持
UITableViewHeaderFooterView

在 IB/. 我同意这是一方的糟糕设计 Apple, 但他惊讶于我是一个粗心,特殊的细节,而是一个小问题,给出了表格景观中的所有问题。 E.g., 令人惊讶的是,毕竟这些年来,我们仍然不能在圆角中制作上部和下页脚的原型,并应该依靠这些 NIB 以及方法注册方法。

但得出的不正确 , 无法使用什么
https://developer.apple.com/do ... ister
, 方法 API, 这是积极使用的 iOS 6. 让我们不要扔孩子和洗澡水。

厘米。

这个答案 Swift 2 版本。

喜特乐

赞同来自:

Rob的答案,虽然听起来很令人信服并忍受了时间的考验,错误的,始终是。 难以孤立在压倒性的收养和许多附录的人群中,但我会试图勇气并讲述真相。

问题是你不能做出一种神奇的方式来转向 UIView 在尖端B. UITableViewHeaderFooterView, 只是在识别检查员那样宣布它。 W. UITableViewHeaderFooterView 有重要的功能是其适当工作的关键,并且简单 UIView, 你会怎么样?



, 他们不是。

在 UITableViewHeaderFooterView 有
https://developer.apple.com/do ... tview
, 所有自定义亚种都必须添加到此,而不是 UITableViewHeaderFooterView.

但 UIView 神秘地扔了 UITableViewHeaderFooterView 缺乏这一点
contentView

在尖端。 因此,当Rob说: "添加您的控件和限制 IB", 他让你添加亚种

直接B. UITableViewHeaderFooterView

, 和

不是

在里面
contentView

. 因此,标题拒绝配置错误。

另一个问题的迹象是你不允许给予 UITableViewHeaderFooterView 背景颜色。 如果您这样做,您将在控制台中收到此消息:

设置背景颜色 UITableViewHeaderFooterView 过时了。 请安装习惯 UIView 使用背景的所需颜色而不是属性 backgroundView.

但是B. nib 你不

你可以

不要在您的背景下设置背景颜色 UITableViewHeaderFooterView, 和你

真的

在控制台中获取此消息。

那么这个问题的正确答案是什么? 不是

没有可能

回答。 Apple 这里做了一个巨大的愚蠢。 他们提供了一种方法,允许您将笔注册为您的来源 UITableViewHeaderFooterView, 但

图书馆中没有对象 UITableViewHeaderFooterView

. 因此,这种方法

无用

.

不可能的

正确设计 UITableViewHeaderFooterView 在尖端。

这是一个巨大的错误 Xcode. 我在此问题上提交了一个错误报告

在 2013

年,他仍然坐在那里,开放。 我将在年复一年逐年重新填写错误 Apple 继续坚持,说: "尚未定义,areas and are are。" 因此,他们认识到错误,但什么都不做。

但是,你是什么

你可以

制作 - 这是创造普通人 UIView 在提示中,然后在代码中 /在你的实施中
viewForHeaderInSection

/ 从提示手动加载视图并将其放入
contentView

你的标题视图。

例如,假设我们想在提示中设计我们的标题,我们在我们想要连接出口的标题中有一个标签
lab

. 然后我们需要用户标题类和用户演示文稿类:


class MyHeaderView : UITableViewHeaderFooterView {
weak var content : MyHeaderViewContent!
}
class MyHeaderViewContent : UIView {
@IBOutlet weak var lab : UILabel!
}


我们注册

班级

我们看来的标题,而不是笔:


self.tableView.register/MyHeaderView.self,
forHeaderFooterViewReuseIdentifier: self.headerID/


在文件形式中

xib

, 我们宣布我们的眼睛 MyHeaderViewContent —

不是

MyHeaderView.


viewForHeaderInSection

我们从尖端拔出视图,将其插入
contentView

标题并设置其链接:


override func tableView/_ tableView: UITableView, 
viewForHeaderInSection section: Int/ -> UIView? {
let h = tableView.dequeueReusableHeaderFooterView/
withIdentifier: self.headerID/ as! MyHeaderView
if h.content == nil {
let v = UINib/nibName: "MyHeaderView", bundle: nil/.instantiate
/withOwner: nil, options: nil/[0] as! MyHeaderViewContent
h.contentView.addSubview/v/
v.translatesAutoresizingMaskIntoConstraints = false
v.topAnchor.constraint/equalTo: h.contentView.topAnchor/.isActive = true
v.bottomAnchor.constraint/equalTo: h.contentView.bottomAnchor/.isActive = true
v.leadingAnchor.constraint/equalTo: h.contentView.leadingAnchor/.isActive = true
v.trailingAnchor.constraint/equalTo: h.contentView.trailingAnchor/.isActive = true
h.content = v
// other initializations for all headers go here
}
h.content.lab.text = // whatever
// other initializations for this header go here
return h
}


这是非常令人讨厌的,但这是你能做的最好的事情。

三叔

赞同来自:

我没有声誉足以对亚光答案的答案添加评论。

在任何情况下,这里唯一缺失的是 - 这删除了所有亚种 UITableViewHeaderFooterView.contentView 在添加新视图之前。 这将在原始状态下重置重新使用的单元格并避免内存泄漏。

喜特乐

赞同来自:

创建文件。 UITableViewHeaderFooterView 和相应的xib文件。


class BeerListSectionHeader: UITableViewHeaderFooterView {
@IBOutlet weak var sectionLabel: UILabel!
@IBOutlet weak var abvLabel: UILabel!
}


在注册表视图单元格时,以与同样的方式注册笔。 提示的名称和重用标识符必须与文件的名称匹配。 /Xib 没有重用标识符。/


func registerHeader {
let nib = UINib/nibName: "BeerListSectionHeader", bundle: nil/
tableView.register/nib, forHeaderFooterViewReuseIdentifier: "BeerListSectionHeader"/
}


Dequeue 并使用相同的单元格。 标识符 - 这是文件的名称。


override func tableView/_ tableView: UITableView, viewForHeaderInSection section: Int/ -> UIView? {

let header = tableView.dequeueReusableHeaderFooterView/withIdentifier: "BeerListSectionHeader"/ as! BeerListSectionHeader

let sectionTitle = allStyles[section].name
header.sectionLabel.text = sectionTitle
header.dismissButton?.addTarget/self, action: #selector/dismissView/, for: .touchUpInside/
return header
}


不要忘记标题高度。


override func tableView/_ tableView: UITableView, heightForHeaderInSection section: Int/ -> CGFloat {
return BeerListSectionHeader.height
}

要回复问题请先登录注册