UITableView单元格返回nil属性值

UITableView单元格返回nil属性值

本文介绍了UITableView单元格返回nil属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if(indexPath.row==0)
    {
        static NSString *CellID = @"FlourishCustomCell";
        FlourishCustomCell *cell = (FlourishCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellID];
        if (cell == nil) {
            cell = [[[FlourishCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID] autorelease];
            cell.frame = CGRectMake(0, 0.0, 292.0, 30);
        }

        id <NSFetchedResultsSectionInfo> sectionInfo =
        [[appDelegate.fetchedResultsController sections] objectAtIndex:indexPath.section];
        NSLog(@"DATE:%@", [sectionInfo name]); //Output: March 25

        cell.dateLabel.text=[sectionInfo name];
        NSLog(@"header cell:%@", cell.dateLabel.text); //Output:header cell:(null)

        return cell;
    }
    else {
        static NSString *CellIdentifier = @"IdeaCustomCell";
        IdeaCustomCell *cell = (IdeaCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[IdeaCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            cell.frame = CGRectMake(0, 0.0, 292.0, 70);
        }

        [self configureCell:cell atIndexPath:[self newIndexPathForIndexPath:indexPath]];
        return cell;
    }
}

对于else部分(IdeaCustomCell),单元格显示并可以正常工作,但是由于某些极其令人沮丧的原因,当我设置FlourishCelldateLabel,然后立即尝试访问该值时,即使设置了它,我也得到一个null值!并且该单元格不会显示在屏幕上.

The cells display and work fine for the else part (the IdeaCustomCell), but for some extremely frustrating reason, when I set the dateLabel of the FlourishCell, and then immediately try to access that value, I get a null value, even though I just set it! And the cell doesn't display on screen.

我尝试覆盖FlourishCustomCell类中dateLabel的setter方法,并在其中放置了NSLog语句,但是由于某种原因它从未被调用.

I tried overriding the setter method for dateLabel in the FlourishCustomCell class, and I put an NSLog statement there, but it never gets called for some reason.

我完全不知道是什么原因造成的.我的意思是我当时和那里都在分配,但它仍然给我空值.有什么想法吗?

I have absolutely no idea what could be causing this. I mean I'm allocating right then and there, but it's still giving me null. Any ideas?

推荐答案

您需要初始化标签

解决方案1:用代码初始化单元格

Solution 1: Initializing cell in code

@interface FlourishCustomCell : UITableViewCell

@property (nonatomic, retain) UILable * dateLabel;

@end

@implementation FlourishCustomCell
@synthesize dateLabel = _dateLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
   if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
   {
      _dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,300,50)];
      [self.contentView addSubview:_dateLabel]
   }

   return self;
}

@end

解决方案2:从笔尖初始化细胞

Solution 2: Initializing cell from nib

@interface FlourishCustomCell : UITableViewCell

@property (nonatomic, retain) UILable * dateLabel;

@end

@implementation FlourishCustomCell
@synthesize dateLabel = _dateLabel;

- (id)init
{
self = [[[[NSBundle mainBundle] loadNibNamed:@"YOUR_NIB_NAME" owner:nil options:nil]
         lastObject]
        retain];

   return self;
}

@end

编辑:糟糕,忘记返回自己的init方法,更新了答案

EDIT: oops, forgot to return self on init method, updated the answer

这篇关于UITableView单元格返回nil属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:19