Ios 第一个分组的表视图节标题未显示
我有一个分组的表视图,我想有自定义的节标题。但是我在显示表视图的第一节标题时遇到问题 我有两个部分,第一部分的部分标题未显示,但第二部分是: 我见过类似的问题,以及那些通过实现Ios 第一个分组的表视图节标题未显示,ios,uitableview,swift2,sections,xcode7.1,Ios,Uitableview,Swift2,Sections,Xcode7.1,我有一个分组的表视图,我想有自定义的节标题。但是我在显示表视图的第一节标题时遇到问题 我有两个部分,第一部分的部分标题未显示,但第二部分是: 我见过类似的问题,以及那些通过实现heightForHeaderInSection解决的问题,但我已经实现了 我将这些部分设置为: let kSectionHeaderContact: String = "CONTACT INFORMATION" let kSectionHeaderFeedback: String = "FEEDBACK
heightForHeaderInSection
解决的问题,但我已经实现了
我将这些部分设置为:
let kSectionHeaderContact: String = "CONTACT INFORMATION"
let kSectionHeaderFeedback: String = "FEEDBACK"
enum Sections: Int {
case ContactSection = 0
case FeedbackSection = 1
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == Sections.ContactSection.rawValue {
return contactObjectsDictionary.count
} else if section == Sections.FeedbackSection.rawValue {
return feedbackObjectsDictionary.count
} else {
return 0
}
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == Sections.ContactSection.rawValue {
sectionHeaderView.sectionHeaderTitle.text = kSectionHeaderContact
} else if section == Sections.FeedbackSection.rawValue {
sectionHeaderView.sectionHeaderTitle.text = kSectionHeaderFeedback
}
return sectionHeaderView
}
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return kContactTableViewSectionHeaderViewHeight
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifierContactTableViewCell, forIndexPath: indexPath) as! ContactTableViewCell
// Configure the cell...
var titleText: String = ""
var detailText: String = ""
if indexPath.section == Sections.ContactSection.rawValue {
let contactObject = contactObjectsDictionary[indexPath.row]
titleText = contactObject[kDictionaryTitleKey]!
detailText = contactObject[kDictionaryDetailKey]!
} else if indexPath.section == Sections.FeedbackSection.rawValue {
let feedbackObject = feedbackObjectsDictionary[indexPath.row]
titleText = feedbackObject[kDictionaryTitleKey]!
detailText = feedbackObject[kDictionaryDetailKey]!
}
cell.configureCell(titleText, detail: detailText)
return cell
}
我正在故事板中实现我的自定义节标题,然后通过出口引用它:
编辑:
我希望能够在故事板中设计自定义节头,而不是以编程方式。问题在viewForHeaderSection方法中。 您需要创建UIView的新实例&需要返回它 或 您可以为UITableViewHeaderHeaderView创建类并重用它 如果您对此有任何疑问,请随时询问 让titleFirstLabel:UILabel={
让label=UILabel()
label.text=“text”
label.frame=CGRect(x:15,y:10,宽度:100,高度:20)
返回标签
}() 和viewDidLoad()添加以下代码 tableView.addSubview(titleFirstLabel)
那对我有用。祝你好运:)但是为什么我不能使用带有@IBOutlet var sectionHeaderView:ContactTableViewSectionHeaderView的故事板中的视图呢!推荐人?它有一个类“ContactTableViewSectionHeaderView”,它是UIView的一个子类,因为它是UIView的唯一实例&您只能使用它一次。或者创建新实例。那么ViewForHeaderSection的工作原理与重用表视图单元格类似吗?但是我应该能够从情节提要中使用自己定制的UIView子类?我想在情节提要中设计它,然后将其实现为节标题。ContactTableViewSectionHeaderView不支持从XIB查看&是的,它的工作原理与重用表视图单元格相同。如果我的回答解决了您的问题,您可以接受向上投票。我不熟悉该技术,但是我认为你应该让用户知道这些修改应该在哪里进行。