UITableView 部分标题故障?
UITableView Section Title Glitch?
我有一个视图控制器,里面有一个分组的表视图。这会产生一个非常奇怪的故障,导致 sectionTitles 显示在旁边。我已经将顶部、尾部、底部和前导约束附加到表视图的超级视图,并且我测试了在 cellForRowAtIndexPath 中返回 UITableViewCell() 以查看是否是导致问题的单元格,但它仍然像这样显示.
var sectionTitles = ["Customer Orders", "Unprocessed Orders", "Processed Orders"]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sectionTitles.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return sectionTitles
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier(CONSTANTS.CELL_IDENTIFIERS.ORDER_CELL) as! OrderTableViewCell
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Perform segue to order list in future
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 117.0
}
几个小时后我发现我没有实现这个方法
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitles[section]
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return sectionTitles
}
如果你只有一个部分,你就不会放很多标题。
相反,如果您想为每个单元格提供标题,则必须在自定义的 tableViewCell 中添加标签,并且在 cellForRowAtIndexPath
中必须放置类似这样的内容(在创建 NSObject
class):
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier(CONSTANTS.CELL_IDENTIFIERS.ORDER_CELL) as! OrderTableViewCell
let order = Order[indexPath.row]
cell.yourTitleLabel.text = order.title
return cell
}
我有一个视图控制器,里面有一个分组的表视图。这会产生一个非常奇怪的故障,导致 sectionTitles 显示在旁边。我已经将顶部、尾部、底部和前导约束附加到表视图的超级视图,并且我测试了在 cellForRowAtIndexPath 中返回 UITableViewCell() 以查看是否是导致问题的单元格,但它仍然像这样显示.
var sectionTitles = ["Customer Orders", "Unprocessed Orders", "Processed Orders"]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sectionTitles.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return sectionTitles
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier(CONSTANTS.CELL_IDENTIFIERS.ORDER_CELL) as! OrderTableViewCell
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Perform segue to order list in future
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 117.0
}
几个小时后我发现我没有实现这个方法
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitles[section]
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return sectionTitles
}
如果你只有一个部分,你就不会放很多标题。
相反,如果您想为每个单元格提供标题,则必须在自定义的 tableViewCell 中添加标签,并且在 cellForRowAtIndexPath
中必须放置类似这样的内容(在创建 NSObject
class):
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier(CONSTANTS.CELL_IDENTIFIERS.ORDER_CELL) as! OrderTableViewCell
let order = Order[indexPath.row]
cell.yourTitleLabel.text = order.title
return cell
}