【问题标题】:Where Do i add my custom tableview cell label in my UITableview when using NSCoder?使用 NSCoder 时,我应该在 UITableview 中的哪里添加自定义表格视图单元格标签?
【发布时间】:2013-03-24 05:48:10
【问题描述】:

使用 NSCoder 时,我应该在 UITableview 中的何处添加自定义 tableview 单元格标签? 我已经创建了 UITableViewCell 类并将所有内容都连接到界面生成器中。

我尝试替换 cell.textLabel.text = oneName.name; with cell.label.text = oneName.name; 并且它只显示一个黑色方块。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    NSUInteger row = [indexPath row];
    DrillObject *oneName = [self.addDrill objectAtIndex:row];
    cell.textLabel.text = oneName.name;



    return cell;
}

【问题讨论】:

  • “使用 NSCoder 时”是什么意思?
  • 我只是说,因为通常的实现方式不起作用 cell.label.text = [addDrill objectAtIndex:indexPath.row];

标签: uitableview xcode4.5 nscoder


【解决方案1】:

您确定您的标签尺寸合适吗?当使用自定义单元格时,我真的推荐使用免费的 Sensible TableView 框架。该框架会自动将您的对象数据加载到自定义标签中,并且还会在需要时自动调整标签/单元格的大小。

【讨论】:

    【解决方案2】:

    有两个选项可以将标签添加到您想要的 CustomCell 类:
    1-在xib文件中的单元格上添加一个标签并为其分配一个标签,然后创建一个像这样的getter:

    -(UILabel*)label
    {
         id label = [self viewByTag:3]; 
         if([label isKindOfClass:[UILabel class]])
         {
                 return label;
         }
         return nil;
    }
    

    2- 在 init 方法中创建标签,不要忘记将标签背景颜色设置为清除颜色,如下所示:

    UILabel *label = [UILabel alloc] initWithFrame:myFreame];
    [label setBackgroundColor:[UIColor clearColor]];
    
    // now you should have property on .h file like this 
    @property (nonatomic, weak) UILabel *label;
    // so back to the init dont forget to do this
    
    _label = label;
    

    就是这样。

    【讨论】:

    • 如果我使用故事板,第一个是一样的,因为它给self.view 一个错误
    • 您遇到了什么错误?如果您从 CustomCell 类执行此操作,则只需执行此操作 [self viewByTag:3] 而无需查看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    相关资源
    最近更新 更多