Coldsteel48
Coldsteel48

Reputation: 3512

UILabel in my custom UITableViewCell doesn't change text properly

I have a custom UITableViewCell inside it I have a label that shows the ammount of licks , when i drawing the cell everything is fine , however when I'm trying to change the label text it overlaps on its self (and it is very sad tbh).

I tried to remove and add it again , also tried to set needsDisplay and it didn't help me :(

here is my code :

in DrawLabel method

//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];

when I do want to change the label's text here in another method

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

Upvotes: 0

Views: 263

Answers (2)

Coldsteel48
Coldsteel48

Reputation: 3512

Okay thanks for help to every1 i have solved the issue the solution was to reload the tableview data .

Upvotes: 0

Massimo Nicolardi
Massimo Nicolardi

Reputation: 136

I find a little mess in your code. I Whould do like the following:

  • Use interface builder to add a UITableViewCell prototype to your table
  • assign (via interface builder) a reuse identifier (for example "cell")
  • inside this UITableViewCell add a UILabel (via interface) and place it wherever you like
  • assign a tag to the UILabel (for example 1)

After this setup you need to implement the table rows.

Here's an example

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

Upvotes: 1

Related Questions