I’m creating a simple messaging app, in which the user can send either an image or a text message. I’m using a UITableView to display the messages, and I’m using HeightForRowAtIndexPath to determine the size of a given cell, depending on whether it has an image in it or not.
However, after sending an image, the next text message (another UITableViewCell) lays itself on top of the image (which is significantly taller than the default height of a UITableViewCell), and I can’t figure out why!
Here’s the code I have for HeightForRowAtIndexPath:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Message *messageInUse = [messageArray objectAtIndex:indexPath.row];
if(messageInUse.message != nil || [[messageInUse message] isEqualToString:@""])
{
CGSize size = [[messageInUse message] sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake([messageTable frame].size.width, [messageTable frame].size.height) lineBreakMode:UILineBreakModeWordWrap];
return size.height + 44;
}
else if(messageInUse.image != nil)
{
return 170;
}
else
return 0;
}
And CellForRowAtIndexPath:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
for(int i = 0; i < [[cell subviews] count]; i++)
{
if([[[cell subviews] objectAtIndex:i] isKindOfClass:[UIImageView class]])
{
cell = nil;
break;
}
}
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
Message *messageInUse = [[messageArray objectAtIndex:indexPath.row] retain];
if([messageInUse message] != nil && ![[messageInUse message] isEqualToString:@""])
{
NSLog(@"Message sent");
[cell.textLabel setText:[messageInUse message]];
[[cell textLabel] setNumberOfLines:0];
[[cell textLabel] setLineBreakMode:UILineBreakModeWordWrap];
[cell.detailTextLabel setText:[messageInUse timeStamp]];
}
else if([messageInUse image] != nil)
{
NSLog(@"Image sent");
[cell setFrame:CGRectMake([cell frame].origin.x, [cell frame].origin.y, [cell frame].size.width, 180)];
//Need to make sure that the date label is under the image
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 2, 96, 126)];
[imageView setContentMode:UIViewContentModeScaleToFill];
[imageView setImage:[messageInUse image]];
[cell addSubview:imageView];
[imageView release];
//[cell.detailTextLabel setText:[messageInUse timeStamp]];
UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 135, 320,20)];
[timeLabel setText:[messageInUse timeStamp]];
[timeLabel setFont:[UIFont systemFontOfSize:14]];
[timeLabel setTextColor:[UIColor grayColor]];
[cell addSubview:timeLabel];
[timeLabel release];
}
else
{
//Something is wrong with the message
[cell.textLabel setText:@"Error occurred. Please resend"];
[cell.detailTextLabel setText:[self getFormattedDate]];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[messageInUse release];
return cell;
}
Any help is much appreciated! Thanks in advance!
It looks like your if statement in
heightForRowAtIndexPathis wrong. It loos like it should read:That’s the same as you have in
cellForRowAtIndexPaththen, which sounds like its what you want. Presumably in your cells which are displaying an image you have themessagestring being""so the if statement is true and it makes the cell 44 pixels high.