【问题标题】:Custom UItableViewCell label is overlapping自定义 UItableViewCell 标签重叠
【发布时间】:2014-05-19 11:15:28
【问题描述】:

使用UITableView 发现cells 没有正确出列。我使用storyboard 创建了原型单元格,并在单元格上设置了UIlabel。

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell==nil) {
    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   }
     UILabel*textLabel = (UILabel*)[cell viewWithTag:11];
    textLabel.font=[UIFont fontWithName:@"MyUnderwood" size:16];
    textLabel.text = [_tableLabelArray objectAtIndex:indexPath.row];

return cell;


}

这是我滚动TableView时得到的UITableView的图像

【问题讨论】:

  • 这是你的真实代码吗?因为没有使用textLabel,也没有多大意义。似乎也没有错。您的自定义单元格中有 uilabel 吗?
  • 您是否偶然在原型单元格中拥有多个具有相同标签的 UILabel?
  • @Justafinger 是的,我的自定义单元格中有 UILabel。
  • 如果您有该方法的真实/完整代码,那就太好了。

标签: ios uitableview storyboard


【解决方案1】:

这可能只是一个小错误。尝试使用不同的标记值,或验证原型单元格中使用的值。

【讨论】:

    【解决方案2】:

    您没有在CellForRowAtIndexPath 中使用您的自定义单元格。代码看起来很好,就在 UITableViewCell 的位置上 像这样将您的自定义单元格出列:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"cell"; //needs to be the identifier you defined in story boards
        YourCustomCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell==nil) {
        cell=[[YourCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
       }
         UILabel*textLabel = (UILabel*)[cell viewWithTag:11];
        textLabel.font=[UIFont fontWithName:@"MyUnderwood" size:16];
        textLabel.text = [_tableLabelArray objectAtIndex:indexPath.row];
    
    return cell;
    
    
    }
    

    然后您需要创建 UITableViewCell 的子类并将其命名为 YourCustomCell 并链接所有 IBOutlet 对象,如标签等。将这行代码放在您的 .h 文件 #import YourCustomCell.h 顶部,然后只需访问该单元格的所有属性像 cell.textLabel 一样,而不是创建标签并将其投射到带有标签的视图。

    【讨论】:

    • 你怎么知道“cell”不是他自定义cell的标识符?
    • 因为他初始化的单元格是 UITableViewCell 类型。这真的可能是个问题
    • 每个自定义单元格都从 UITableViewCell 继承,因此只有当他尝试访问自定义单元格的自定义属性时才会出现问题,而这里似乎不是这种情况
    • 我没有创建自定义单元格,而是使用prototypeCell,而不是创建插座,我使用标签来表示每个元素。那么有什么区别呢?
    • 不同之处在于您在情节提要中创建了一个 customTableview 单元格。此单元格显示在您的 tableView 中并转换为UITableViewCell,这会使您的标签重叠。在故事板中拥有自定义单元格,您很可能必须创建 UITableViewCell 的子类并在那里进行所有自定义
    【解决方案3】:

    我通过使用“nil”而不是 cellIdentifier 解决了这个问题

    例如: 而不是

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    

    使用这个

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:nil];
    

    它对我有用

    【讨论】:

    • 它给了我错误。 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
    • 确保你返回你的单元格,你把 nil 放在标识符中,而不是在返回语句中
    【解决方案4】:

    您只需要在情节提要中勾选标签的“清除图形上下文”属性即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多