自定义UITableViewCell中的UILabel不';不要正确地更改文本

自定义UITableViewCell中的UILabel不';不要正确地更改文本,uitableview,uilabel,Uitableview,Uilabel,我有一个自定义的UITableViewCell在它里面我有一个标签,上面显示了点击的数量, 当我绘制单元格时,一切都很好,但是当我试图更改标签文本时,它自身会重叠(非常遗憾tbh) 我尝试删除并再次添加它,还尝试设置needsDisplay,但它对我没有帮助:( 这是我的密码: 内置标签法 //we will start from the labels: licks and comments _licksLabel = [[UILabel alloc]initWithFrame:CGRectMa

我有一个自定义的
UITableViewCell
在它里面我有一个标签,上面显示了点击的数量, 当我绘制单元格时,一切都很好,但是当我试图更改标签文本时,它自身会重叠(非常遗憾tbh)

我尝试删除并再次添加它,还尝试设置
needsDisplay
,但它对我没有帮助:(

这是我的密码:

内置标签法

//we will start from the labels: licks and comments
_licksLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, self.frame.size.width, 40)];
[_licksLabel setText:[NSString stringWithFormat:@"%@ %@",[_dict objectForKey:@"loves"],LocalizedString(@"licks")]];

[_licksLabel setFont:[UIFont fontWithName:FONT_REGULAR size:15]];

[_licksLabel sizeToFit];
[_licksLabel setTextColor:UIColorFromRGB(grey4)];
[self addSubview:_licksLabel];
当我确实想在这里用另一种方法更改标签的文本时

- (void)meLike
{

if ([[_dict objectForKey:@"self_loved"]integerValue]>0)
{
  //  return;
}
[_licksLabel removeFromSuperview];
[FeedAPI loveFeedWithId:[_dict objectForKey:@"id"]];
[_dict setValue:[NSNumber numberWithInt:1] forKeyPath:@"self_loved"];
[_dict setValue:[NSNumber numberWithInt:[[_dict objectForKey:@"loves"]integerValue]+1] forKeyPath:@"loves"];
[_licksLabel setText:[NSString stringWithFormat:@"%@ %@",[_dict objectForKey:@"loves"],LocalizedString(@"licks")]];
[_licksButton setImage:[UIImage imageNamed:@"ic_lick_on.png"] forState:UIControlStateNormal];
[self setNeedsDisplay];
[super setNeedsDisplay];
[self addSubview:_licksLabel];
}

我发现你的代码有点乱。我会这样做:

  • 使用界面生成器将UITableViewCell原型添加到表中
  • 分配(通过接口生成器)重用标识符(例如“单元”)
  • 在这个UITableViewCell中添加一个UILabel(通过接口),并将其放置在您喜欢的任何地方
  • 将标记指定给UILabel(例如1)
完成此设置后,您需要实现表行

这里有一个例子

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *cell;

   cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

   UILabel *name = (UILabel *) [self.tableView viewWithTag:1];
   name.text =  @"whateveryouwant;

   return cell;
}

好的谢谢大家的帮助我已经解决了这个问题
解决方案是重新加载tableview数据。

我想你没有回答这个问题,我用文本等绘制单元格没有问题,但是当我试图更改现有文本时,新文本会覆盖现有文本,而不会重新绘制它……因此我尝试了几种方法来解决这个问题:-/你的答案更像是:当uilables结束时滚动时重叠,这不是我的情况,我也不喜欢界面生成器,抱歉也许我没有发现问题,但如果文本与旧文本重叠,它会像两个UILabel一样一个接一个地缝合。这就是为什么我试图描述“经典”通过interface builder实现这一点的方法。如果您想以编程方式进行此操作,您可以自由地重新检查某些代码是否未运行两次。如果您发布整个代码,我可以查看一下,否则我不知道如何帮助您。没关系,朋友,我已经解决了,问题在于重新加载表数据……无论如何,感谢您的帮助:)