I have a custom cell with 3 labels and one button. Everything worked well until I didn't implement tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
method for my table view. (I need it because I set the cell height here based on the length of the labels.) The problem is when I tap likeButton
the likeCountLabel.text
will display the labels sample value from the IB for a sec and then shows the previous (correct) string again in every cell, not just in the tapped. When I remove heightForRowAtIndexPath:
it's perfect again. As I discovered it happens after I reload the table view, but don't have any idea, because these labels work perfectly when I don't adjust the height.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"cell";
NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
PFQuery *query = [like query];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (objects) {
NSString *likeNumber = [NSString stringWithFormat:@"%d", objects.count];
cell.likeCountLabel.text = likeNumber;
}
}];
cell.likeButton.tag = indexPath.row;
[cell.likeButton addTarget:self action:@selector(didTapLikeButton:) forControlEvents:UIControlEventTouchUpInside];
cell.usrName.text = object[@"usrName"];
cell.usrDescription.text = object[@"desc"];
cell.date.text = object[@"date"];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"cell";
NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
PFObject *object = [self.objects objectAtIndex:indexPath.row];
cell.usrName.text = object[@"usrName"];
cell.usrDescription.text = object[@"desc"];
cell.date.text = object[@"date"];
//get the height the cell
// [cell layoutIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
//paddign of 1 Point
return height + 12;
}
- (void)didTapLikeButton:(id)sender {
// Cast Sender to UIButton
UIButton *button = (UIButton *)sender;
PFObject *likeRow = [self.objects objectAtIndex:button.tag];
[SaveHelper likeObject:likeRow withUploader:[PFUser currentUser]];
[self.tableView reloadData];
}