UITableView SMS Style Bubbles

Discussion in 'iOS Programming' started by DSchwartz88, Jul 23, 2008.

  1. macrumors 6502

    May 18, 2006
    Hey All,

    Im having a bit of trouble getting a UITableView to display correctly.

    I have images of SMS Style bubbles, and would like the UITableView to display them as such. Right now i have the table displaying with just text, and it looks pretty much like a normal table.

    lb1 and lb2 are my labels, and they call lb1.font, lb1.textColor, and etc. I was wondering if there is such a thing as lb1.backgroundPicture or lb1.background. i know there is lb1.backgroundColor, but i need a picture not a single color as my background. Let me know if anyone has any help. Also im having trouble bringing in the picture as a UIImage object, so i can put it in. Any help would be great!

  2. macrumors member

    May 24, 2003
    How about...

    I haven't tried this, but this might be a way around it:

    Add a UIImageView as a subview to each table cell, set its size to be the size of the cell, and set its image property to be the relevant bubble UIImage. That should take care of displaying the bubble.

    In order to show the text, apply it as one or more labels, which are also set as subviews of the table cell. Set their backgroundColor properties to be [UIColor clearColor], and the underlying bubble image will show through them. That should give you a bubble with text in it.

    As far as your problems with UIImage are concerned, can you be more specific?
  3. thread starter macrumors 6502

    May 18, 2006

    Im having trouble, both importing an image into UIImageView and setting UIImageViews size, here is my code:

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *aqua = [bundle pathForResource:mad:"aqua" ofType:mad:"png"];
    UIImage *bubble1 = [[UIImage alloc] initWithContentsOfFile: (NSString *)aqua];

    UIImageView *bubble2 = [[[UIImageView alloc] initWithFrame:CGRectMake(5.0, 28.0, 280.0, 15.0)]autorelease];
    [bubble2 setImage:bubble1];

    [cell.contentView addSubview:bubble2];

    Still it doesnt show up once i go to this UITableView.

    Any help?
  4. macrumors member

    May 24, 2003
    I'd be tempted to try something like this:

    UIImage *bubble1 = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"aqua" ofType:@"png"]];
    UIImageView *bubble2 = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 28.0, 280.0, 15.0)];
    bubble2.image = bubble1;
    [cell addSubview:bubble2];
    return cell;
    I'm assuming that you're doing this inside the -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath delegate method. Although, if you're intending to re-use aqua.png multiple times, you'd probably do better to set it up in your application delegate, rather than keep reinitialising it every time your table calls the delegate method. And you'll probably want to consider some memory management in that little bit of code, too. I didn't when I thought it up. :)
  5. thread starter macrumors 6502

    May 18, 2006
    EDIT: got a bit farther


    So the table now shows up again (had nothing to do with code, the xml files werent loading, but thats fixed now).

    But now the bubbles still dont show up, the program compiles fine and runs fine, it just looks the same as before, no bubble.

    Any help?

    Thanks again
  6. macrumors member

    May 24, 2003
    I'm afraid that I will need to see your code before I can help much more...

    For what it's worth, here's the code of a quick and dirty project that I knocked together just to test what I was doing:
    //  ImagesInTableAppDelegate.m
    //  ImagesInTable
    #import "ImagesInTableAppDelegate.h"
    @implementation ImagesInTableAppDelegate
    @synthesize window;
    - (void)applicationDidFinishLaunching:(UIApplication *)application {	
    	contentArray = [[NSArray alloc] initWithObjects:@"One", @"Two", @"Three", nil];
    	bubble1 = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Image_1" ofType:@"png"]];
        [window makeKeyAndVisible];
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
    	return [contentArray count];
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    	return 1;
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    	if (cell == nil) {
    		cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0.0, 0.0, 0.0, 0.0) reuseIdentifier:@"cellID"] autorelease];
    	UIImageView *bubble2 = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 40.0)];
    	bubble2.image = bubble1;
    	UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 3.0, 280.0, 21.0)];
    	titleLabel.textColor = [UIColor redColor];
    	titleLabel.backgroundColor = [UIColor clearColor];
    	titleLabel.text = [contentArray objectAtIndex:indexPath.row];
    	[cell addSubview:bubble2];
    	[cell addSubview:titleLabel];
    	return cell;
    - (void)dealloc {
    	[window release];
    	[super dealloc];
    It might give you an idea of what could be causing your problem.

Share This Page