Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6863481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:47:17+00:00 2026-05-27T02:47:17+00:00

I’m creating a simple messaging app, in which the user can send either an

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:47:17+00:00Added an answer on May 27, 2026 at 2:47 am

    It looks like your if statement in heightForRowAtIndexPath is wrong. It loos like it should read:

    if(messageInUse.message != nil && ![[messageInUse message] isEqualToString:@""])
    

    That’s the same as you have in cellForRowAtIndexPath then, which sounds like its what you want. Presumably in your cells which are displaying an image you have the message string being "" so the if statement is true and it makes the cell 44 pixels high.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I have a text area in my form which accepts all possible characters from
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.