• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - UITableView 单元格返回 nil 属性值

[复制链接]
菜鸟教程小白 发表于 2022-12-12 19:52:20 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有以下几点:

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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 cellnull)

        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)的单元格显示和工作正常,但是由于某些非常令人沮丧的原因,当我设置 dateLabel FlourishCell,然后立即尝试访问该值,我得到一个 null 值,即使我只是设置它!并且单元格不显示在屏幕上。

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

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



Best Answer-推荐答案


你需要初始化标签

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

@interface FlourishCustomCell : UITableViewCell

@property (nonatomic, retain) UILable * dateLabel;

@end

@implementation FlourishCustomCell
@synthesize dateLabel = _dateLabel;

- (id)initWithStyleUITableViewCellStyle)style reuseIdentifierNSString *)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:从 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 方法中返回 self,更新了答案

关于iphone - UITableView 单元格返回 nil 属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9865609/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap