Uitableview 如何使用故事板实现自定义表视图节页眉和页脚
不使用故事板,我们只需将Uitableview 如何使用故事板实现自定义表视图节页眉和页脚,uitableview,ios5,uistoryboard,Uitableview,Ios5,Uistoryboard,不使用故事板,我们只需将UIView拖到画布上,将其布局,然后在tableView:viewForHeaderInSection或tableView:viewforfooterInstitution委托方法中进行设置 如果故事板不能将UIView拖到画布上,我们如何实现这一点我提出的解决方案基本上与引入故事板之前使用的解决方案相同 创建一个新的空接口类文件。将UIView拖到画布上,根据需要进行布局 手动加载nib,在viewForHeaderInSection或viewForFooterInS
UIView
拖到画布上,将其布局,然后在tableView:viewForHeaderInSection
或tableView:viewforfooterInstitution
委托方法中进行设置
如果故事板不能将UIView拖到画布上,我们如何实现这一点我提出的解决方案基本上与引入故事板之前使用的解决方案相同 创建一个新的空接口类文件。将UIView拖到画布上,根据需要进行布局 手动加载nib,在viewForHeaderInSection或viewForFooterInSection委托方法中指定适当的页眉/页脚节
我曾希望苹果通过故事板简化这种情况,并继续寻找更好或更简单的解决方案。例如,自定义表的页眉和页脚可以直接添加。我过去常常执行以下操作来创建页眉/页脚视图:
- 将节页眉/页脚的自由形式视图控制器添加到情节提要
- 在视图控制器中处理标题的所有内容
- 在表视图控制器中,为重新填充
[NSNull null]
- 在ViewForHeaderInstition/ViewForFooterInstitution中,如果视图控制器尚不存在,请使用序列图像板实例化EviewController标识符创建它,在数组中记住它并返回视图控制器视图
- 添加一个额外的单元格,并将所需的元素放入其中李>
- 将标识符设置为特定的内容(在我的示例中是SectionHeader)
- 实现
方法或tableView:viewForHeaderInSection:
方法tableView:viewforfootensection:
- 使用
获取标题[tableView dequeueReusableCellWithIdentifier:
- 执行
方法tableView:heightForHeaderInSection:
-(UIView*)表格视图:(UITableView*)表格视图标题部分:(NSInteger)部分{
静态NSString*CellIdentifier=@“SectionHeader”;
UITableViewCell*headerView=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(headerView==nil){
[NSException raise:@“headerView==nil..”格式:@“没有从故事板加载具有匹配CellIdentifier的单元格”];
}
返回headerView;
}
编辑:如何更改标题(注释问题):
tableView:viewForHeaderInSection:
方法中,通过调用以下命令获取标签:UILabel*label=(UILabel*)[headerView视图带标记:123];
[label setText:@“新标题”];
我在iOS7中使用故事板中的原型单元使其工作。在我的自定义节标题视图中有一个按钮,可以触发在情节提要中设置的序列
从
正如pedro.m所指出的,问题在于轻触节头会导致选择节中的第一个单元格
正如Paul Von指出的,这是通过返回单元格的contentView而不是整个单元格来解决的
然而,正如Hons指出的那样,长时间按下该节标题将使应用程序崩溃
解决方案是从contentView中删除任何手势识别器
-(UIView*)表格视图:(UITableView*)表格视图标题部分:(NSInteger)部分{
静态NSString*CellIdentifier=@“SectionHeader”;
UITableViewCell*sectionHeaderView=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
while(sectionHeaderView.contentView.GestureRecognitors.count){
[sectionHeaderView.contentView removeGestureRecognizer:[sectionHeaderView.contentView.gestureRecognizers对象索引:0];
}
返回sectionHeaderView.contentView;}
如果您在节标题视图中没有使用手势,那么这个小技巧似乎可以解决问题。接下来,我将介绍如何使标题与带有显示指示器的正常行一样可选择
我在标题的原型单元格中添加了一个子类为UIButton的按钮(子类名称“ButtonWithArgument”),并删除了标题文本(粗体的“title”文本是原型单元格中的另一个UILabel)
然后将按钮设置为整个标题视图,并添加一个带有
-(UIView*)表格视图:(UITableView*)表格视图标题部分:(NSInteger)部分
{
静态NSString*CellIdentifier=@“PersonGroupHeader”;
UITableViewCell*headerView=(UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(headerView==nil)
{
[NSException raise:@“headerView==nil,PersonGroupTableViewController”格式:[NSString stringWithFormat:@“情节提要没有标识符为%@,单元标识符为的原型单元]];
}
// https://stackoverflow.com/a/24044628/3075839
while(headerView.contentView.gestureRecognizers.count)
{
[headerView.contentView removeGestureRecognizer:[headerView.contentView.gestureRecognizers objectAtIndex:0];
}
ButtonWithArgument*按钮=(ButtonWithArgument*)[headerView视图带标记:4];
button.frame=headerView.bounds;//将点击区域设置为整个标题视图
button.argument=[[NSNumber alloc]initWithInteger:section];//来自ButtonWithArguments子类
[按钮添加目标:自我操作:@选择器(headerView
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return tableView.dequeueReusableCell(withIdentifier: "CustomHeader")
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 75
}
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
static NSString *CellIdentifier = @"SectionHeader";
SettingsTableViewCell *sectionHeaderCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
sectionHeaderCell.myPrettyLabel.text = @"Greetings";
sectionHeaderCell.contentView.backgroundColor = [UIColor whiteColor]; // don't leave this transparent
return sectionHeaderCell.contentView;
}
class CustomHeaderFooterView: UITableViewHeaderFooterView {
var cell : UITableViewCell? {
willSet {
cell?.removeFromSuperview()
}
didSet {
if let cell = cell {
cell.frame = self.bounds
cell.autoresizingMask = [UIViewAutoresizing.FlexibleHeight, UIViewAutoresizing.FlexibleWidth]
self.contentView.backgroundColor = UIColor .clearColor()
self.contentView .addSubview(cell)
}
}
}
self.tableView.registerClass(CustomHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "SECTION_ID")
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = self.tableView.dequeueReusableHeaderFooterViewWithIdentifier("SECTION_ID") as! CustomHeaderFooterView
if view.cell == nil {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell")
view.cell = cell;
}
// Fill the cell with data here
return view;
}
class myViewController: UIViewController {
var header: [UILabel] = myStringArray.map { (thisTitle: String) -> UILabel in
let headerView = UILabel()
headerView.text = thisTitle
return(headerView)
}
extension myViewController: UITableViewDelegate {
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return(header[section])
}
}
class TP_TaskViewTableViewSectionHeader: UITableViewCell{
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableCell(withIdentifier: "header", for: IndexPath.init(row: 0, section: section))
return header
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "DataCell") as! DataCell
cell.data1Label.text = "DATA KEY"
cell.data2Label.text = "DATA VALUE"
return cell
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 75
}
// Example of regular data cell dataDelegate to round out the example
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DataCell", for: indexPath) as! PlayerCell
cell.data1Label.text = "\(dataList[indexPath.row].key)"
cell.data2Label.text = "\(dataList[indexPath.row].value)"
return cell
}