繁体   English   中英

自定义UItableViewCell标签重叠

[英]Custom UItableViewCell label is overlapping

使用UITableView并发现cells无法正确出队。我使用storyboard UIlabel创建了原型单元,并在单元UIlabelUIlabel

  - (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;


}

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

在此处输入图片说明

您没有在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.textLabel而不是创建标签并将其强制转换为带有标签的视图。

这可能只是一个小错误。 尝试使用其他标签值,或验证原型单元中正在使用的值。

我通过使用“ nil”代替cellIdentifier解决了此问题

例如:代替

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

用这个

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:nil];

它对我有用

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM