Code Bye

自定义tableviewcell但是给cell里的label赋值后还是nil

 

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
是在这个函数里对label进行赋值的。。不知道哪里出错了>,<

我先建议一下, 这个 label 最后不要在 cellForRow 里实例化,可以放在 Cell 里,让 Cell 自己去管理它。
回到这个问题,你在哪里判断的?把 cellForRow 实例化的代码和你判断时用的代码发上来吧。
把你的问题再完善一下。说明白label是如何创建的。是通过code的方式还是xib的方式。
引用 1 楼 zhangao0086 的回复:

我先建议一下, 这个 label 最后不要在 cellForRow 里实例化,可以放在 Cell 里,让 Cell 自己去管理它。
回到这个问题,你在哪里判断的?把 cellForRow 实例化的代码和你判断时用的代码发上来吧。

这个是自定义的cell。label是在xib里创建的
@interface MapTableCell : UITableViewCell{
    UILabel *poiNO;
    UILabel *poiName;
    UILabel *poiAddress;
    UIImageView *poiImageView;
}
    @property (nonatomic ,retain) IBOutlet UILabel *poiNO;
    @property (nonatomic ,retain) IBOutlet UILabel *poiName;
    @property (nonatomic ,retain) IBOutlet UILabel *poiAddress;
    @property (nonatomic ,retain) IBOutlet UIImageView *poiImageView;
@end

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellWithIdentifier = @”TableCellIdentifier”;
    MapTableCell *cell = (MapTableCell *)[tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (cell == nil) {
        cell = [[MapTableCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellWithIdentifier];
    }
    
    NSUInteger row = [indexPath row];
    BMKPoiInfo *info = [_poiInfoList objectAtIndex:row];
    cell.poiName.text = [NSString stringWithFormat:@”%lu%@”,(unsigned long)row,info.name];
    cell.poiAddress.text = [info.city stringByAppendingString:info.address];
    return cell;
}

引用 2 楼 zhanglei5415 的回复:

把你的问题再完善一下。说明白label是如何创建的。是通过code的方式还是xib的方式。

这个是自定义的cell。label是在xib里创建的
@interface MapTableCell : UITableViewCell{
    UILabel *poiNO;
    UILabel *poiName;
    UILabel *poiAddress;
    UIImageView *poiImageView;
}
    @property (nonatomic ,retain) IBOutlet UILabel *poiNO;
    @property (nonatomic ,retain) IBOutlet UILabel *poiName;
    @property (nonatomic ,retain) IBOutlet UILabel *poiAddress;
    @property (nonatomic ,retain) IBOutlet UIImageView *poiImageView;
@end

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellWithIdentifier = @”TableCellIdentifier”;
    MapTableCell *cell = (MapTableCell *)[tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (cell == nil) {
        cell = [[MapTableCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellWithIdentifier];
    }
    
    NSUInteger row = [indexPath row];
    BMKPoiInfo *info = [_poiInfoList objectAtIndex:row];
    cell.poiName.text = [NSString stringWithFormat:@”%lu%@”,(unsigned long)row,info.name];
    cell.poiAddress.text = [info.city stringByAppendingString:info.address];
    return cell;
}


20分
你的 Cell 创建方法有错:
if (cell == nil) {
        cell = [[MapTableCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellWithIdentifier];
}
这样 init 的必然是一个和 xib 毫无关系的 cell,你应该 load 一个 xib,从而实例化一个 cell:
[[NSBundle mainBundle] loadNibNamed:@”CellXibName” owner:nil options:nil].firstObject
把 CellXibName 换成你自己的。
—————————分隔符—————————
这样虽然可以解决问题,但不用这么麻烦,你试下新的 APIs。
提前注册为 tableView 注册好一个 Cell:
[self.tableView registerNib:[UINib nibWithNibName:@”CellXibName” bundle:nil] forCellReuseIdentifier:CELL_IDENTIFIER];
你就不用担心 cell 是否为 nil 了,如果 cell 为 nil 的话,tableView 会自己创建一个:
cell = [tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER forIndexPath:indexPath];
这里虽然是用的复用,但是 tableView 会内部判断是否有可以重用的 cell。

旧的 APIs 方式(虽然自己实例化一个 Cell) 建议就不要用了,麻烦,你用下新的 APIs(其实也不新,iOS 6开始就有了),有问题再发出来。

Have fun!


20分
引用 4 楼 spoonysnail 的回复:
Quote: 引用 2 楼 zhanglei5415 的回复:

把你的问题再完善一下。说明白label是如何创建的。是通过code的方式还是xib的方式。

这个是自定义的cell。label是在xib里创建的
@interface MapTableCell : UITableViewCell{
    UILabel *poiNO;
    UILabel *poiName;
    UILabel *poiAddress;
    UIImageView *poiImageView;
}
    @property (nonatomic ,retain) IBOutlet UILabel *poiNO;
    @property (nonatomic ,retain) IBOutlet UILabel *poiName;
    @property (nonatomic ,retain) IBOutlet UILabel *poiAddress;
    @property (nonatomic ,retain) IBOutlet UIImageView *poiImageView;
@end

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellWithIdentifier = @”TableCellIdentifier”;
    MapTableCell *cell = (MapTableCell *)[tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (cell == nil) {
        cell = [[MapTableCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellWithIdentifier];
    }
    
    NSUInteger row = [indexPath row];
    BMKPoiInfo *info = [_poiInfoList objectAtIndex:row];
    cell.poiName.text = [NSString stringWithFormat:@”%lu%@”,(unsigned long)row,info.name];
    cell.poiAddress.text = [info.city stringByAppendingString:info.address];
    return cell;
}

xib与这些IBOutlet 的插座变量建立连线没有? 还有就是建议使用registerNib, registerClass让系统来托管cell的复用及创建

[self.tableView registerNib:[UINib nibWithNibName:@"MapTableCell" bundle:nil]   forCellReuseIdentifier:@"TableCellIdentifier"];

这样在 cellForRowAtIndexPath 中就可以这样来拿到cell 

MapTableCell *cell = (MapTableCell *)[tableView dequeueReusableCellWithIdentifier:CellWithIdentifier forIndexPath:indexPath];

不需要再对cell是否为nil 进行判断。系统当从重用队列中失败会根据你指定的方式(nib or class)来自动创建。

引用 5 楼 zhangao0086 的回复:

你的 Cell 创建方法有错:
if (cell == nil) {
        cell = [[MapTableCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellWithIdentifier];
}
这样 init 的必然是一个和 xib 毫无关系的 cell,你应该 load 一个 xib,从而实例化一个 cell:
[[NSBundle mainBundle] loadNibNamed:@”CellXibName” owner:nil options:nil].firstObject
把 CellXibName 换成你自己的。
—————————分隔符—————————
这样虽然可以解决问题,但不用这么麻烦,你试下新的 APIs。
提前注册为 tableView 注册好一个 Cell:
[self.tableView registerNib:[UINib nibWithNibName:@”CellXibName” bundle:nil] forCellReuseIdentifier:CELL_IDENTIFIER];
你就不用担心 cell 是否为 nil 了,如果 cell 为 nil 的话,tableView 会自己创建一个:
cell = [tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER forIndexPath:indexPath];
这里虽然是用的复用,但是 tableView 会内部判断是否有可以重用的 cell。

旧的 APIs 方式(虽然自己实例化一个 Cell) 建议就不要用了,麻烦,你用下新的 APIs(其实也不新,iOS 6开始就有了),有问题再发出来。

Have fun!

O(∩_∩)O谢谢~

引用 6 楼 zhanglei5415 的回复:
Quote: 引用 4 楼 spoonysnail 的回复:
Quote: 引用 2 楼 zhanglei5415 的回复:

把你的问题再完善一下。说明白label是如何创建的。是通过code的方式还是xib的方式。

这个是自定义的cell。label是在xib里创建的
@interface MapTableCell : UITableViewCell{
    UILabel *poiNO;
    UILabel *poiName;
    UILabel *poiAddress;
    UIImageView *poiImageView;
}
    @property (nonatomic ,retain) IBOutlet UILabel *poiNO;
    @property (nonatomic ,retain) IBOutlet UILabel *poiName;
    @property (nonatomic ,retain) IBOutlet UILabel *poiAddress;
    @property (nonatomic ,retain) IBOutlet UIImageView *poiImageView;
@end

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellWithIdentifier = @”TableCellIdentifier”;
    MapTableCell *cell = (MapTableCell *)[tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (cell == nil) {
        cell = [[MapTableCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellWithIdentifier];
    }
    
    NSUInteger row = [indexPath row];
    BMKPoiInfo *info = [_poiInfoList objectAtIndex:row];
    cell.poiName.text = [NSString stringWithFormat:@”%lu%@”,(unsigned long)row,info.name];
    cell.poiAddress.text = [info.city stringByAppendingString:info.address];
    return cell;
}

xib与这些IBOutlet 的插座变量建立连线没有? 还有就是建议使用registerNib, registerClass让系统来托管cell的复用及创建

[self.tableView registerNib:[UINib nibWithNibName:@"MapTableCell" bundle:nil]   forCellReuseIdentifier:@"TableCellIdentifier"];

这样在 cellForRowAtIndexPath 中就可以这样来拿到cell 

MapTableCell *cell = (MapTableCell *)[tableView dequeueReusableCellWithIdentifier:CellWithIdentifier forIndexPath:indexPath];

不需要再对cell是否为nil 进行判断。系统当从重用队列中失败会根据你指定的方式(nib or class)来自动创建。

谢谢啦~~


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明自定义tableviewcell但是给cell里的label赋值后还是nil