如何自定义UITableView的Header

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何自定义UITableView的Header相关的知识,希望对你有一定的参考价值。

参考技术A 如何自定义UITableView的Header

转载

分类: iphone开发笔记

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
UIView *v_headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 23)];//创建一个视图(v_headerView)
UIImageView *v_headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 23)];//创建一个UIimageView(v_headerImageView)
v_headerImageView.image = [UIImage imageNamed:@"ip_top bar.png"];//给v_headerImageView设置图片
[v_headerView addSubview:v_headerImageView];//将v_headerImageView添加到创建的视图(v_headerView)中
[v_headerImageView release];//释放v_headerImageView所占用的资源
UILabel *v_headerLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 1, 100, 19)];//创建一个UILable(v_headerLab)用来显示标题
v_headerLab.backgroundColor = [UIColor clearColor];//设置v_headerLab的背景颜色
v_headerLab.textColor = [UIColor grayColor];//设置v_headerLab的字体颜色
v_headerLab.font = [UIFont fontWithName:@"Arial" size:13];//设置v_headerLab的字体样式和大小
v_headerLab.shadowColor = [UIColor whiteColor];//设置v_headerLab的字体的投影
[v_headerLab setShadowOffset:CGSizeMake(0, 1)];//设置v_headerLab的字体投影的位置
//设置每组的的标题
if (section == 0)
v_headerLab.text = @"拍品导航";

if (section == 1)
v_headerLab.text = @"专场特卖";

[v_headerView addSubview:v_headerLab];//将标题v_headerLab添加到创建的视图(v_headerView)中
[v_headerLab release];//释放v_headerLab所占用的资源
return v_headerView;//将视图(v_headerView)返回
本回答被提问者和网友采纳

如何解决滚动具有自定义单元格的 UITableview 的崩溃?

【中文标题】如何解决滚动具有自定义单元格的 UITableview 的崩溃?【英文标题】:How to solve crashes on scrolling UITableview which has custom cell? 【发布时间】:2015-10-26 06:21:28 【问题描述】:

我的应用在滚动具有自定义单元格但在模拟器上运行良好的 UITableview 时崩溃。

下面是我的代码

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath // Values of each cell

    
        static NSString *cellidentifier = @"cellidentifier";
        TableCell *cell = (TableCell*) [tableView dequeueReusableCellWithIdentifier:cellidentifier];
        //NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
        //cell = [nib objectAtIndex:0];
        cell = (TableCell *) [[[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil] objectAtIndex:0];

        if (cell == nil) 
        
            cell = [[[TableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier] autorelease];                

        

        cell.lblname.text = [name objectAtIndex:indexPath.row];
        cell.lblphone.text = [phone objectAtIndex:indexPath.row];
        cell.lbladdress.text =[address objectAtIndex:indexPath.row];

        NSData * photodata=[photo objectAtIndex:indexPath.row];
        cell.imgphoto.image=[UIImage imageWithData:photodata];        

        return cell;

任何帮助将不胜感激。

【问题讨论】:

能否在控制台中包含崩溃消息? 错误“收到内存警告”,但在模拟器上运行良好,在 iphone 上崩溃 我认为因为你没有重复使用你的单元格,所以如果你有很多单元格,它会迅速增加内存使用量,并且在模拟器上它有大量内存,而在设备上却没有。看看我的回答。 【参考方案1】:

您的 tableView 根本不重用单元格。因为您在调用dequeueReusableCellWithIdentifier: 后重新分配了cell。你的代码应该是:

static NSString *cellidentifier = @"cellidentifier";
TableCell *cell = (TableCell*) [tableView dequeueReusableCellWithIdentifier:cellidentifier forIndexPath:indexPath];
//NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil];
//cell = [nib objectAtIndex:0];

if (!cell) 
    cell = (TableCell *) [[[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil] objectAtIndex:0];

【讨论】:

它说“实例方法 dequeueReusableCellWithIdentifier:forIndexPath:indexPath 未找到(返回类型默认为 id)” 您的部署目标是什么?哪个版本的iOS?此方法在 iOS 6.0 中可用。我查了一下,这里没有错字。当您键入该行时,Xcode 是否会为您提供该方法的自动完成功能? 即使在 xcode 上运行它也会显示错误提示“UITableView dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized selector sent to instance 0xf3e3800 2015-10-26 13:00:10.233 Rotary[383:707] ***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]:无法识别的选择器发送到实例 0xf3e3800” ***第一掷调用堆栈:(0x35b6988f 0x3378d259 0x35b6ca9b 0x35b6b915 0x35ac6650 0xdac93 0x332c9efb 0x332c8fd9 0x332c8763 0x3326cf37 0x35ac81fb 0x32c93aa5 0x32c936bd 0x32c97843 0x32c9757f 0x32c8f4b9 0x35b3db1b 0x35b3bd57 0x35b3c0b1 0x35abf4a5 0x35abf36d 0x3385a439 0x33297cd5 0xd659b 0xd6530)终止称为从调试器抛出exceptionMessage : 第k个包发送失败"

以上是关于如何自定义UITableView的Header的主要内容,如果未能解决你的问题,请参考以下文章

github上关于iOS的各种开源项目集合(转)

如何区分具有相同自定义单元格的两个 UITableView

如何自定义 UITableView 的标题背景颜色、高度和文本?

如何使用自定义 UITableView 上传图片

如何解决滚动具有自定义单元格的 UITableview 的崩溃?

如何自定义 UITableView 中部分的 UIView?