UITableViewCell分配问题 - 单元格不是零 [英] UITableViewCell allocation issue - cell is not nil

查看:60
本文介绍了UITableViewCell分配问题 - 单元格不是零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
static NSString * CellIdentifier = @Cell2;
UILabel * titleLabel;
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,5,300,20)];
[cell.contentView addSubview:titleLabel];
titleLabel.tag = 0011;
}
else
{
titleLabel =(UILabel *)[cell.contentView viewWithTag:0011];
}

//配置单元格...
NSMutableString * title = [NSMutableString stringWithString:@Customer:];
[title appendString:[titles objectAtIndex:indexPath.row]];
titleLabel.text = title.copy;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = UITextAlignmentLeft;
titleLabel.font = [UIFont systemFontOfSize:18.0];

我的单元格永远不会是零,而我的titleLabel,因为它从未被分配,尽管我的单元格是产生。我看不出这是怎么可能的。 if状态永远不会是真的,应该是第一次生成的单元格,但我的单元格应该是按原样创建的,而不是我的titleLabel的

解决方案

听起来好像您使用的是iOS 5(或更高版本)和故事板。



在iOS 5(或更高版本)下,如果如果您使用的是Storyboard和TableView控制器,则保证 dequeueReusableCellWithIdentifier:方法返回一个单元格(前提是您已在故事板中定义了具有给定标识符的单元格)。 / p>

如果是这种情况,解决方案是在Storyboard中完全创建自定义表格单元格。转到故事板中的表格视图,选择内容:动态原型并制作原型单元格:1 。现在以图形方式布置单元格,使其完全符合您的要求。现在单击单元格并设置标识符:Cell2 。您现在不需要在运行时创建标签或检查它是否为零。有关如何引用已设置的标签的完整详细信息,请参阅iOS 5发行说明(下面的链接)或网络上的许多教程。



查看iOS 5发布备注部分配置表格视图


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell2";
    UILabel *titleLabel;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 300, 20)];
        [cell.contentView addSubview:titleLabel];
        titleLabel.tag = 0011;
    }
    else 
    {
        titleLabel = (UILabel *)[cell.contentView viewWithTag:0011];
    }

    // Configure the cell...
    NSMutableString *title = [NSMutableString stringWithString:@"Customer: "];
    [title appendString:[titles objectAtIndex:indexPath.row]];
    titleLabel.text = title.copy;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textAlignment = UITextAlignmentLeft;
    titleLabel.font = [UIFont systemFontOfSize:18.0];

My cell is never nil, and my titleLabel, because of that never got allocated, although my cells are generated. I can not see how this is possible. The if state is never true, which should be for the cells that are being generated for the first time, but my cells are created as they should be, without my titleLabel's

解决方案

It sounds as if you are using iOS 5 (or later) and Storyboards.

Under iOS 5 (or later), if you are using Storyboards and the TableView Controller, the dequeueReusableCellWithIdentifier: method is guaranteed to return a cell (provided that you have defined a cell with the given identifier in the Storyboard).

If this is the case, the solution is to fully create the custom table cell in the Storyboard. Go to your Table View in your Storyboard, select Content:Dynamic Prototypes and make the Prototype Cells:1. Now layout your cell graphically to be exactly what you want. Now click on the cell and set Identifier:Cell2. You will now not need to create the label at runtime or check if it is nil. Full details including how to reference the labels you have setup are in the iOS 5 Release Notes (link below) or in many tutorials on the web.

See the iOS 5 Release Notes section Configuring Table Views

这篇关于UITableViewCell分配问题 - 单元格不是零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆