【问题标题】:Custom cell label returns nil自定义单元格标签返回 nil
【发布时间】:2015-05-31 08:54:21
【问题描述】:

我有一个表格视图,我已经将自定义单元格加载到它。

但是,单元格组件不会显示。 (标签返回 nil - 见下图)

我已经创建了很多 tableview 并且过去嵌入了自定义单元格。但是,我遇到了一个问题,我无法找到解决方案。

代码如下:

索引路径处的行单元格

static NSString *CellIdentifier = @"cell";

    MyCell *cell = (MyCell *) [tableView  dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleSubtitle   reuseIdentifier:CellIdentifier];

    }



    cell.myLabel.text = @"fhjfhjdhfjdfhd ";

    return cell;

自定义单元格 .h 文件

#import <UIKit/UIKit.h>



@interface MyCell : UITableViewCell{



}

@property (weak, nonatomic) IBOutlet UILabel * myLabel;




@end

自定义单元格 .m 文件

#import "MyCell.h"



@implementation MyCell

@synthesize myLabel;



- (void)awakeFromNib {

    // Initialization code

}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];



    // Configure the view for the selected state

}

@end

故事板

更新

有一个项目https://github.com/honcheng/PaperFold-for-iOS

在侧边栏上,滑动时会出现一个表格视图。

这是不显示任何自定义标签组件的表格视图。

想知道这些信息是否有助于找到问题。

【问题讨论】:

  • 我认为你应该在声明属性时保留标签,将弱改为强
  • 还有什么是 MyCell 中的 LeftCell *cell = (LeftCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; ?
  • 你没有注册课程吧?
  • @SanjayMohnani,对于 IBOutlets,weak 没问题,因为视图(在这种情况下为标签)由其父视图保留。
  • Tableview 给出单元格标识符名称

标签: ios objective-c uitableview


【解决方案1】:

如果您使用情节提要来自定义单元格。

你不需要在 tableView 中分配单元格:cellForRowAtIndexPath:

这样使用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    return cell;
}

【讨论】:

  • 您希望我删除cell==nil 条件并仅添加您在上面提供的代码吗?然后它崩溃了。说UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
  • 您是否为您的单元格设置了标识符?
  • 我已经用更多信息更新了我的帖子。请参阅上面的原始问题。
  • 我认为我的代码中还有另一个问题。我添加了一个 github 项目,并且自定义单元格没有添加到那里。见上面的帖子。谢谢。
【解决方案2】:

如果您使用 xib 创建了自定义单元,那么您需要在创建单元时加载该 nib-

static NSString *cellIdentifier = @"cellIdentifier";
CustomCell *cell = nil;
        cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if(cell == nil)
        {
            cell = (CustomCell *)[[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] lastObject];

        }

[cell.label setText:@"label text"];

【讨论】:

  • 我使用故事板创建的。是一样的吗?
【解决方案3】:

请使用以下代码

   if (cell == nil) {

        NSArray *nib=[[NSBundle mainBundle] loadNibNamed:@"YourCustomCellName" owner:self options:nil];
        cell=[nib objectAtIndex:0];  //index of your custom cell
    }

注意:在将字符串分配给该标签之前,请检查您的自定义单元格是否分配了内存。并且您的自定义单元格已连接到“自定义类”(右侧检查器中有第三列)

请参考随附的屏幕截图。

【讨论】:

  • 截图在哪里。我想你忘了附上它
  • 我已经添加了自定义单元格类。但是我该如何分配内存呢?
  • 分配nib文件时自动分配内存
  • cell=[nib objectAtIndex:0];
  • 检查标签中的断点并检查单元格和标签是否分配了内存。谢谢。
【解决方案4】:

试试这个, 然后为您的标签设置标签

UILabel *lblName=[[UILabel alloc]initWithFrame:CGRectZero];
lblName=(UILabel *)[cell.contentView viewWithTag:5];

将您的值分配给标签

【讨论】:

  • 您是否在情节提要中为单元格分配了 MyCell 类?
猜你喜欢
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
  • 2016-06-13
  • 1970-01-01
  • 2013-06-07
  • 2017-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多