如何取到uitableview的section自定义的sectionheader

> ipad开发
UITableView的section headerTitle问题
UITableView的section headerTitle问题
nwyyfx & &
发布时间: & &
浏览:0 & &
回复:0 & &
悬赏:0.0希赛币
[img=http://my.csdn.net/my/album/detail/1287872][/img]ipad开发,在实现一个UITableView多个section的时候,给每个sction赋值headTitle的时候,老师赋值两边,如上图所示.1.-&(NSString&*)tableView:(UITableView&*)tableView&titleForHeaderInSection:(NSInteger)section{&&&&NSString&*&&&if(section==0)&&&&{&&&&&&&&str=@"网站子站点";&&&&}&&&&if(section==1)&&&&{&&&&&&&&str=@"列表库";&&&&}&&&&&&&&&&&&&&&&return&}2.//-&(UIView&*)tableView:(UITableView&*)tableView&viewForHeaderInSection:(NSInteger)section//{//&&&&UIView&*viewSection=//&&&&if(section==0)//&&&&{//&&&&//viewSection&=&[[UIView&alloc]&initWithFrame:CGRectMake(10,&0,&300,&20)];//&&&&&&&&viewSection&=&[[UIView&alloc]&init];//&&&&&&&&viewSection.backgroundColor=[UIColor&clearColor];////&&&&&&&&&&&&&&&&&&&&&&&&&&&UILabel&*textSection&=&[[UILabel&alloc]&initWithFrame:CGRectMake(10,2,&140,&20)];////&&&&&&&&&&&&&&&&&&&&&&&&&&&//textSection.text&=&@"网站工作区";////&&&&&&&&&&&&&&&&&&&&&&&&&&&textSection.textColor&=[UIColor&clearColor];////&&&&&&&&&&&&&&&&&&&&&&&&&&&textSection.backgroundColor&=&[UIColor&clearColor];////&&&&&&&&&&&&&&&&&&&&&&&&&&&textSection.font&=&[UIFont&systemFontOfSize:20.0f];////&&&&&&&&&&&&&&&&&&&&&&&&&&&[viewSection&addSubview:textSection];////&&&&&&&&&&&&&&&&&&&&&&&&&&&[textSection&release];//&&&&&&&&&&&&&&&&&&&&&&&&&&&//&&&&}//&&&&if(section==1)//&&&&{//&&&&&&&&viewSection&=&[[UIView&alloc]&init];//&&&&&&&&viewSection.backgroundColor=[UIColor&clearColor];////&&&&&&&UILabel&*textSection&=&[[UILabel&alloc]&initWithFrame:CGRectMake(10,2,&140,&20)];////&&&&&&&//textSection.text&=&@"列表库";////&&&&&&&textSection.textColor&=[UIColor&redColor];////&&&&&&&textSection.backgroundColor&=&[UIColor&
看不到图啊!!rdhhb & &
& & (0)(0)图在哪?lixiang002 & &
& & (0)(0)
本问题标题:
本问题地址:
温馨提示:本问题已经解决,不能解答。
暂无合适的专家
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&[ios]UITableView headerViewForSection 返回 (空值)
我有 UITableView 与 2 个部分。每个都有它自己的 headerView 。我已经创建了一个自定义 headerView 通过 -viewForHeaderInSection: 方法。后来,我计划有点修改它,所以我需要使用 viewForHeader 方法,但不能访问 headerView 和它有 subViews 。
作为一个简单的例子,我在试着 NSLog viewForHeader 中的对象 -didSelectRowAtIndexPath: 但我得到 (null) 的结果。
示例代码:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
return 75;
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
UIView *myHeader = [[UIView alloc] init];
switch (section) {
[myHeader setBackgroundColor:[UIColor greenColor]];
[myHeader setBackgroundColor:[UIColor redColor]];
UILabel *myLabel = [[UILabel alloc] init];
[myLabel setFrame:CGRectMake(10, 0, 100, 30)];
[myLabel setTag:101];
[myLabel setBackgroundColor:[UIColor clearColor]];
[myLabel setText:[NSString stringWithFormat:@"Section: %d",section]];
[myHeader addSubview:myLabel];
return myH
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewHeaderFooterView *testView = [self.tableView headerViewForSection:indexPath.section];
NSLog(@"%@",testView);
//displays (null)
需要创建自定义 UIView 作为 xib headerView 吗?(因为每一个类似的问题和文档")
To make the table view aware of your header or footer view, you need to register it.
You do this using the registerNib:forCellReuseIdentifier: or
registerClass:forCellReuseIdentifier: method of UITableView.
解决方法 1:
好的在一些试验和错误后我终于解决我自己的困境。我做了 headerView 只会一样一个单元格。
为一个单元格,我们会采取 UITableViewCell 和使用dequeueReusableCellWithIdentifier虽然......为的页眉页脚,我们将采取 UITableViewHeaderFooterView ,并使用 dequeueReusableHeaderFooterViewWithIdentifier 方法。
剩下的就很多相同的概念作为一个单元格。
先决条件:
设置的页眉的高度到 40
设置的节数为 2 或更多
每节要至少 1 个设置的行数
iOS6 + ( UITableViewHeaderFooterView 与 iOS5 和下面不会工作)
第一种方法:
创建和使用默认的 UITableViewHeaderFooterView 内 -viewForHeaderInSection: 方法:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
static NSString *HeaderIdentifier = @"header";
UITableViewHeaderFooterView *myHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
if(!myHeader) {
myHeader = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
UIButton *btnUp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnUp setTag:101];
[btnUp setTitle:@"-" forState:UIControlStateNormal];
[btnUp setFrame:CGRectMake(tableView.frame.size.width - 35, 5, 30, 30)];
[myHeader addSubview:btnUp];
[myHeader.textLabel setText:[NSString stringWithFormat:@"Section: %d",section]];
[myHeader setFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];
return myH
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewHeaderFooterView *theHeaderView = [tableView headerViewForSection:indexPath.section];
NSLog(@"%@",theHeaderView); // -- great! ... not (null) anymore
UIButton *theButton = (UIButton *)[theHeaderView viewWithTag:101];
[theButton setTitle:@"+" forState:UIControlStateNormal];
第二种方法:
使用自定义 UITableViewHeaderFooterView 子类:
创建 UITableViewHeaderFooterView 子类并将它命名为CustomHeaderView
创建了一个视图界面 nib 文件的类
Xib,在选定视图 & 在中它是身份检查器
指定的自定义类CustomHeaderView
作的性质、 合成和连接他们的 xib
@property (strong, nonatomic) IBOutlet UILabel *lblS
@property (strong, nonatomic) IBOutlet UIButton *btnS
修改 -viewForHeaderInSection: & -didSelectRowAtIndexPath: 作为:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
static NSString *HeaderIdentifier = @"header";
CustomHeaderView *myHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
if(!myHeader) {
[tableView registerClass:[CustomHeaderView class] forHeaderFooterViewReuseIdentifier:HeaderIdentifier];
myHeader = [[[NSBundle mainBundle] loadNibNamed:@"CustomHeaderView"
owner:self
options:nil] objectAtIndex:0];
[myHeader.btnSomething setTitle:@"-" forState:UIControlStateNormal];
[myHeader.lblSomething setText:[NSString stringWithFormat:@"Section: %d",section]];
return myH
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
CustomHeaderView *theHeaderView = (CustomHeaderView*)[tableView headerViewForSection:indexPath.section];
NSLog(@"%@",theHeaderView);
[theHeaderView.lblSomething setAlpha:theHeaderView.lblSomething.alpha-0.1];
[theHeaderView.btnSomething setTitle:@"+" forState:UIControlStateNormal];
PS: 与问题 UITableViewHeaderFooterView 是它是 iOS6 + 如果,任何原因,您的标头/必须是 UIView ,然后看看下一个方法
使用一个简单的 UIView :
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
UIView *vwHeader = [[UIView alloc] init];
[vwHeader setTag:200 + section]; //[1] first method
switch (section) {
[vwHeader setBackgroundColor:[UIColor greenColor]];
[vwHeader setBackgroundColor:[UIColor redColor]];
UILabel *lblTitle = [[UILabel alloc] init];
[lblTitle setFrame:CGRectMake(10, 0, 100, 30)];
[lblTitle setTag:100 + section]; //[2] alternative method
[lblTitle setBackgroundColor:[UIColor clearColor]];
[lblTitle setText:[NSString stringWithFormat:@"Section: %d",section]];
[vwHeader addSubview:lblTitle];
return vwH
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
UIView *vwTest = [self.tableView viewWithTag:200 + indexPath.section]; //[1]
NSLog(@"[1] : %@",vwTest);
UILabel *lblTest = (UILabel *)[self.tableView viewWithTag:100 + indexPath.section]; //[2]
NSLog(@"%@",lblTest.text);
UIView *vwTestForSuperview = lblTest.
NSLog(@"[2] : %@",vwTestForSuperview);
PS:我知道此代码不会服务于任何伟大的目的,但这只是示例的其他人。TableView自定义header和footer-Swift基础教程 - swift迷
在TableView中可以自定义Header和footer,在这篇教程中将使用一些国家名称数据填充tableview并且通过自定义UIVIews来创建Header和footer。这篇教程在iOS8.1和Xcode6.1编译通过。
打开Xcode,新建项目选择Single View Application,Product Name填写IOS8SwiftHeaderFooterTutorial,Organization Name和Organization Identifier自行填写,选择与iPhone设备。
移除默认的ViewController同时拖拽TableViewController至Storyboard上,选中TableViewController嵌入NavigationController并设置NavigationController为初始ViewController。双击TableViewController上的Navigation Bar设置Title为"Countries".选中TableViewCell然后到Attributes Inspector面板设置Identifier为"Cell"。
删除ViewController.swift文件并选择“iOS->Source->Cocoa Touch Class”新建继承UITableViewController的TableViewController类
打开Storyboard选中 Table View Controller然后到属性面板设置对象的自定义Class为为刚才创建的“TableViewController”。如下图
打开TableViewController.swift创建如下属性
var countriesinEurope = ["France","Spain","Germany"]
var countriesinAsia = ["Japan","China","India"]
var countriesInSouthAmerica = ["Argentia","Brasil","Chile"]
实现对应的delegate方法:
override func numberOfSectionsInTableView(tableView: UITableView) -& Int {
// Return the number of sections.
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -& Int {
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -& UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
switch (indexPath.section) {
cell.textLabel?.text = countriesinEurope[indexPath.row]
cell.textLabel?.text = countriesinAsia[indexPath.row]
cell.textLabel?.text = countriesInSouthAmerica[indexPath.row]
//return sectionHeaderView
cell.textLabel?.text = "Other"
return cell
1.设置TableView有3个Section
2.设置每个Section有3行数据
3.赋值每行Cell的显示数据
编译并运行项目,界面将大致如下:
接下来我们将会给每个Section增加header和footer.这步可以通过代码或者Interface Builder完成,现在我们将通过 Interface Builder来创建header.在项目增加继承于UITableViewCell的名为CustomHeaderCell类。
打开Storyboard选择TableView.然后打开 Attribute Inspector改变Prototype Cells数为2.
选中第二个Prototype Cell打开Identity Inspector面板设置prototype Cell的自定义class为CustomHeaderCell
打开Attribute Inspector并设置Identifer为HeaderCell
拖拽Label控件至Prototype Cell上并设置水平上下居中,打开Assistant Editor同时确保 CustomHeaderCell.swift可见,Ctrl+Drag方式给Label创建如下Outlet
打开TableViewController.swift实现如下代码
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -& UIView? {
headerCell = tableView.dequeueReusableCellWithIdentifier("HeaderCell") as CustomHeaderCell
headerCell.backgroundColor = UIColor.cyanColor()
switch (section) {
headerCell.headerLabel.text = "Europe";
//return sectionHeaderView
headerCell.headerLabel.text = "Asia";
//return sectionHeaderView
headerCell.headerLabel.text = "South America";
//return sectionHeaderView
headerCell.headerLabel.text = "Other";
return headerCell
现在每个Section都创建了CustomHeaderCell同时Label赋值对应的值,接着我们通过代码方式创建section的footer,如下:
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -& UIView? {
let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
footerView.backgroundColor = UIColor.blackColor()
return footerView
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -& CGFloat {
return 40.0
上面代码创建一个高度为40背景颜色为黑色的View。在tableView:heightForFooterInSection设置高度。编译运行项目,header和footer都会显示出来,效果如下
feiin 于 1 年前
Swift开发交流QQ群:查看:2073|回复:1
初级工程师
在UITableView中如何添加section header呢?
助理工程师
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)
这三个委托方法5057人阅读
转载自:/hellocby/archive//2513379.html
刚才碰到一个问题,就是把记下来吧:
在自定义uitableview的headview时,有可能有这个需求,即某一个section不需要,但按下边的方法如果只是加上 &&
if(section == 0) & {}
这样的话,貌似还是会有一个默认的section的headview,解决办法是在 & &heightForHeaderInSection 这个方法里把height设成0
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
& & if (section == 0) {
& & & & return 0;
& & }else{
& & & & return&...........;
- (UIView&*)tableView:(UITableView&*)tableView&
viewForHeaderInSection:(NSInteger)section
& &&UIView&*headerView = [[UIViewalloc]&initWithFrame:CGRectMake(10,&0,&300,&30)];//创建一个视图&&
& &&UIImageView&*headerImageView = [[UIImageViewalloc]&initWithFrame:CGRectMake(10,&0,&300,&30)];
& &&UIImage&*image = [UIImageimageNamed:@&4-2.png&];
& & [headerImageView&setImage:image];
& & [headerView&addSubview:headerImageView];
& & [headerImageView&release];
& &&UILabel&*headerLabel = [[UILabelalloc]&initWithFrame:CGRectMake(130,&5,&150,&20)];
& & headerLabel.backgroundColor&= [UIColorclearColor];
& & headerLabel.font&= [UIFontboldSystemFontOfSize:15.0];
& & headerLabel.textColor&= [UIColorblueColor];
& & headerLabel.text&=&@&Section&;
& & [headerView&addSubview:headerLabel];
& & [headerLabel&release];
& &&return&headerV
}//自定义section的头部
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1216444次
积分:15881
积分:15881
排名:第348名
原创:409篇
转载:567篇
评论:78条
(8)(4)(17)(9)(5)(11)(7)(22)(3)(12)(38)(32)(20)(5)(17)(35)(10)(24)(15)(8)(28)(13)(11)(28)(26)(20)(43)(55)(13)(14)(25)(12)(19)(37)(64)(29)(31)(7)(30)(12)(45)(19)(17)(23)(8)(11)(36)}

我要回帖

更多关于 自定义sectionheader 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信