UITableView调用headerViewForSection返回nil问题

本文转载自http://blog.csdn.net/reylen/article/details/47043571,顺便加了一些笔记

在通过 -tableView: viewForHeaderAtSection: 设置headerView,然后再去通过headerViewForSection取的时候 ,发现返回结果为nil;
究其原因

For a Header/Footer, we will take UITableViewHeaderFooterView and use the dequeueReusableHeaderFooterViewWithIdentifier method.1

换一种说法就是:

Header/Footer也要像UITableViewCell一样指定Identifier,否则你用headerViewForSection:方法来获取Head/Footer的结果只能为nil

eg:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{    static NSString *hIdentifier = @"hIdentifier";

    UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:hIdentifier];    if(header == nil) {
        header = [[[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:hIdentifier] autorelease];        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,40)]autorelease];
        label.textColor = [UIColor blackColor];
        [header addSubView:label];
    }    
    NSArray*subview = [header subviews];    
    UILabel* label = nil;    
    for (id v in subview) {        
    if ([v isKindOfClass:[UILabel class]]) {
            label = (UILabel *)v;            
            break;
        }
    }    
    if(label)
    {       // update label info
       label.text = @"label text";
       label.tag = section;
    }    
    return header;
}

那么在需要用到的地方可以通过如下获取到label;

UITableViewHeaderFooterView *headerView = [tableView headerViewForSection:indexPath.section];
NSArray*subview = [header subviews];UILabel* label = nil;for (id v in subview) 
{    
    if ([v isKindOfClass:[UILabel class]]) {
        label = (UILabel *)v;        
        break;
    }
}
if(label)
{   
    // update label info
}
时间: 2024-08-01 03:09:58

UITableView调用headerViewForSection返回nil问题的相关文章

jQuery的ajax调用webservice返回XML数据传参错误

jQuery的ajax调用webservice返回XML数据传参错误: 有时候使用jquery的ajax调用带有参数的webservice返回XML格式输出的时候,会出现传参错误,当然错误的原因可能是多种多样的,下面就简单介绍一种. 一.错误代码: 1.ajax代码: $.ajax({ type:"post", url:"_service.asmx/getDataFromATable", data:" { tablename: temp }",

[转]jQuery调用ASPX返回json

本文转自:http://www.cnblogs.com/fire-phoenix/archive/2009/11/13/1614146.html 本文介绍如何在ASP.NET(ASP.NET/AJAX)里使用基于JQuery的AJAX技术.(源代码下载见最后) 在使用JQuery前,请到www.jquery.com下载最新版本的js代码,然后再代码里使用 <script src="_scripts/jQuery-1.3.2.js" type="text/javascri

解决 pathForResource 返回 nil的问题

点击(此处)折叠或打开 NSString* path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"xml"]; NSLog(@"path=%@",path); NSError *error=nil; NSString* fileText = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncod

调用AJAX返回JSON、XML数据类型

1.调用AJAX返回JSON数据 用下拉列表显示Nation表民族名称 主页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <he

shell调用函数返回值深入分析

编写shell脚本过程中,我们经常会自定义一些函数,并根据函数的返回值不同来执行相应的流程,那么我们如何来获取函数的返回值呢? 首先shell中调用函数有两种方式: 第一种:value=`function_name [arg1 arg2 ......]` 或 第二种:function_name [arg1 arg2 ......] echo $? 这两种有什么区别呢? 举个例子来说: [[email protected] ~]# cat test.sh #!/bin/sh function aa

解决NSData转NSString返回nil的问题

在使用initWithData等方法将NSData转换成NSString时,如果NSData的内容含有非encoding编码的字符,将会返回nil. ----------SDK文档如下------------- - (instancetype)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding; Return Value An NSString object initialized by converting the b

SpringMVC中通过@ResponseBody返回对象,Js中调用@ResponseBody返回值,统计剩余评论字数的js,@RequestParam默认值,@PathVariable的用法

1.SpringMVC中通过@ResponseBody返回对象,作为JQuery中的ajax返回值 package com.kuman.cartoon.controller; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.commons.io.filefilter.FalseFileFilter; import org.slf4j.Logger; import org.s

jQuery调用WebService返回JSON数据

相信大家都比较了解JSON格式的数据对于ajax的方便,不了解的可以从网上找一下这方面的资料来看一下,这里就不多说了,不清楚的可以在网上查一下,这里只说一下因为参数设置不当引起的取不到返回值的问题. 在用jQuery调用WebService的时候,它contentType默认为 以下是WebService服务端的代码: 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.W

Xcode bug: imageNamed:方法返回nil

imageNamed:方法返回nil往往有以下几种情况: 项目不存在该图片: 图片命名不正确: 图片的后缀名有问题(往往使用.png的图片,使用.jpg后缀的图片可能会出问题): 设备使用的是Retina屏幕,但是项目没有提供相应的@2x图片: 排除了以上几种常见情况,我的imageNamed:方法仍然返回nil. 最后,把应用从simulator中卸载,退出simulator,关闭并重新启动Xcode,clean项目,重新编译,运行!悲剧的还是不行. 难道是编辑器出问题了?于是,我把代码注释了