Ios 自定义UItableViewCell标签重叠

Ios 自定义UItableViewCell标签重叠,ios,uitableview,storyboard,Ios,Uitableview,Storyboard,使用UITableView,发现单元格未正确出列。我使用故事板创建了原型单元格,并在单元格上添加UIlabel - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"cell"; UITableViewCell *cell=[tableView deq

使用UITableView,发现单元格未正确出列。我使用故事板创建了原型单元格,并在单元格上添加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的图像


您没有在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文件的顶部导入YourCustomCell.h,只需访问该单元格的所有属性,如cell.textLabel,而不是创建标签并将其强制转换为带标签的视图。

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

我通过使用nil而不是cellIdentifier解决了此问题

例如: 而不是

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
用这个

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:nil];

它帮了我一把。

你只需要勾选故事板中标签的clears graphic context属性。

这是你的真实代码吗?因为textLabel没有被使用,也没有多大意义。这似乎也没有错。您的自定义单元格中是否有uilabel?使用此链接您是否碰巧在原型单元格中有多个uilabel,并且具有相同的标记?@Justafinger是的,我的自定义单元格中有uilabel。如果有该方法的真实/完整代码,那就太好了。您如何知道该单元格不是其自定义单元格的标识符?因为他初始化的单元格是类型为UITableViewCell。这可能真的是一个问题,每个自定义单元格都继承自UITableViewCell,因此只有当他尝试访问自定义单元格的自定义属性时,这才应该是一个问题,这里的情况似乎不是这样,我使用prototypeCell创建自定义单元格,而不是创建Outlet,我使用标记来表示每个元素。那么区别是什么呢?区别是在情节提要中创建了一个customTableview单元格。此单元格显示在tableView中,并强制转换为UITableViewCell,从而使标签重叠。如果在故事板中有自定义单元格,您很可能需要创建UITableViewCell的子类,并在其中进行所有自定义,这会给我带来错误。”UITableView数据源必须从tableView:cellForRowAtIndexPath返回一个单元格:确保返回的单元格已将nil放在标识符中,而不是return语句中