【问题标题】:Custom UITableViewCell does not show the labels in storyboard自定义 UITableViewCell 不显示情节提要中的标签
【发布时间】:2012-07-05 13:06:23
【问题描述】:

在此屏幕截图中,您可以看到我在 UIViewController 中添加了 UITableView,然后通过在其中添加一些标签来自定义 UITableViewCell。但问题是当我运行应用程序时。所有单元格都是空的。根本没有标签。

我不明白可能是什么问题。我已经在网上搜索并阅读了教程,但无法解决。

【问题讨论】:

  • 如何将单元格添加到表格视图中?你能粘贴你的表格视图委托方法吗?

标签: iphone xcode uitableview uistoryboard


【解决方案1】:

我自己解决了这个问题,经过一点努力。
实际上,当您创建自定义单元格时,您必须执行以下操作:

  1. 在情节提要单元格上设置标签、图像等。
  2. 创建一个自定义单元格类(从 UITableViewCell 继承)(CustomCell.h & .m),CustomCell.h 具有标签、图像等的 iboutlet 的所有属性,并在实现中将它们全部合成。
  3. 创建此自定义类后返回情节提要,选择自定义单元格,将其类更改为 CustomCell 并为其指定一些标识符,例如“MyCustomCell”,然后右键单击自定义单元格并将 IBOutlets 与标签等连接起来。
  4. 现在在您实现 UITableView 的类中导入 CustomCell 类,并使用您在 CustomCell 类中定义的属性。

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *MyIdentifier = @"MyCustomCell";
    
        CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                           reuseIdentifier:MyIdentifier] autorelease];
        }
    
        // Here we use the provided setImageWithURL: method to load the web image
        // Ensure you use a placeholder image otherwise cells will be initialized with no image
        [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
                       placeholderImage:[UIImage imageNamed:@"placeholder"]];
            cell.myCustomLabel.text = @"My Text";
        return cell;
    }
    

我做了这一切,我的问题得到了解决,请不要忘记连接代表和数据源和表格。

希望这对其他人有所帮助。

【讨论】:

  • 不应该 [UITableViewCell alloc] 实际上是 [CustomCell alloc]
  • 重要的部分是单元格标识符。确保 Storyboard 和 tableView:cellForRowAtIndexPath 方法中的相同。
【解决方案2】:

这有点晚了,但您可以通过将视图的背景颜色设置为情节提要中的“清除颜色”来解决您的问题。

【讨论】:

    【解决方案3】:

    tableview的delegate方法中,cellforrowatindexpath里面有一个uitableviewcell,在这个cell的初始化中应该有一个标识符。可能是“cell”或“c​​ellIdentifier”。

    您只需要从情节提要中选择您的单元格并将此标识符字符串输入到情节提要中,其中 uitableviewcell 的属性检查器所在的位置。

    希望这会有所帮助.. :)

    【讨论】:

      【解决方案4】:

      在情节提要@"Cell"中首先设置单元格标识符

      然后设置标签的标签

       - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      
      // Create
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
      }
      
      
      
      UILabel *lblDate = (UILabel*)[cell viewWithTag:101];
      UILabel *lblDistance = (UILabel*)[cell viewWithTag:102];
      UILabel *lbltype = (UILabel*)[cell viewWithTag:103];
      
      lblDate.text = [temp objectAtIndex:indexPath.row] valueForKey:@"date"] stringByReplacingOccurrencesOfString:@"-" withString:@"/"]];
      lblDistance.text = [NSString stringWithFormat:@"%@ KM",[[temp objectAtIndex:indexPath.row] valueForKey:@"distance"]];
      
      NSString *type = [NSString stringWithFormat:@"%@",[[temp objectAtIndex:indexPath.row] valueForKey:@"distance"]];
      if([type isEqualToString:@"0"])
      {
          lbltype.text = @"Personal";
      }
      else
      {
          lbltype.text = @"Bussiness";
      }
      // Configure
      return cell;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-19
        • 2015-09-13
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 2020-05-07
        相关资源
        最近更新 更多