Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
Hi, I have a table view and I can successfully add a an image to the left side of it using the 'cell.imageView.image = newImage;'.

What I am trying to do is add a UILabel ontop of this image. The tableView holds clients and in the clients there are X number of jobs for that client. If there are 3 jobs that are currently happening for that client I would like the integer 3 appear over the image in the tableView.

I have been at it for a few hours now with no luck. I think my solution is to add them together first and then add that as a UIImage, but I am not getting it? any ideas?

Psudo code
Make UILabel
Make UIImage
Add UILabel to UIImage
Add UIImage to UITableView cell.
Return cell

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        
    }
    NSDictionary *tempDict = [[NSDictionary alloc] initWithDictionary:[clientListForTable objectAtIndex:indexPath.row]];

    cell.textLabel.text = [tempDict objectForKey:@"client"];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:18.0];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:16.0];
    cell.textLabel.textColor = [UIColor whiteColor];
    
    cell.detailTextLabel.text = [tempDict objectForKey:@"businessName"];
    cell.detailTextLabel.textColor = [UIColor whiteColor];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    CGRect boxRect = CGRectMake(0, 0, 44, 44);
    UIImage *newImage = [UIImage imageNamed:@"greenSquare.png"];
    UILabel *numLabel = [[UILabel alloc] initWithFrame:boxRect];
    numLabel.text = @"3";
    numLabel.textColor = [UIColor blackColor];
    numLabel.backgroundColor = [UIColor clearColor];
    
    //How to add the UILabel to the UIImage
    cell.imageView.image = newImage;

    return cell;
}
 
OMG. I can't believe that is so simple. I should probably review all the UIView / UIImage / UIImageView docs again.

The one place I seem to get lost is when to use the []. Right now I have been going off of memory. so these 2 lines

Code:
    [cell.imageView addSubview:numLabel];
    cell.imageView.image = newImage;

When I started learning Object C I remember reading that part that you can use the DOT syntax or use the []. [Object method] or Object.method. Something tells me that this does not apply to that naming convention?

Both of those lines of code do essentially the same thing. So why not say [cell.imageView addSubview newImage]; ?
 
Dot syntax can be used to replace your setters and getters.

Setters
Code:
object.attribute = value
is the same as
Code:
[object setAttribute:value];

Setters will specifically have the word set in the method name.

Getters
Code:
value = object.attribute;
is the same as
Code:
value = [object attribute];

Dot syntax can't be used to replace other methods.

Messages that you're passing to objects go in []s.

Those two lines of code are not the same.

Code:
[cell.imageView addSubview:numLabel];
This will add another subview to imageView. The previous image will not be altered, but the new subview will be added on top will be added onto of it.

Code:
cell.imageView.image = newImage;
The old previous image will be discarded and a new image will be put in its place.

Also, images and views are not the same thing. I can't think of a technical difference right now so I'll just use this analogy that I just came up with: views are like picture frames as images are like pictures. Without a picture frame, you can't mount a picture; likewise, without an view (more specifically an imageView) you can't display an image.

Hope all this helps.
 
Last edited:
Yes, that jogged the memory. The DOT syntax was for setters and getters, I remember that now. Also I remember reading that the [] were for working with objects, that's right.

so the code
Code:
cell.imageView.image = newImage;
would be [Object.PictureFrame.Picture = UIImage]; using your analogy? Would that be [Object.attribute.attribute = aNewImage]; ?
 
Code:
cell.imageView.image = newImage;
is equivalent to
Code:
[[cell imageView] setImage:newImage]

Dot syntax is just an alternative way to call accessor methods. Without dot syntax, calling accessor methods involves sending messages to objects using [] just like calling any other methods.
 
I get it. I see what is happening now. Thanks for the detailed explanation.
 
Code:
[Object.PictureFrame.Picture = UIImage];
[Object.attribute.attribute = aNewImage];

Neither of those are correct... []s surround messages; = is an assignment.

This is a message:
Code:
[object method];

You might set some value to be equal to the results of the message:
Code:
value = [object method];

The = will never be inside of the []s.

Edit:

Just some examples of messages with arguments
Code:
[object method:argument];
[object methodWithArgumentOne:firstArgument andArgumentTwo:secondArgument];
value = [object methodWithArgumentOne:firstArgument andArgumentTwo:secondArgument];

:s precede each argument.

Hope this helps.
 
Thanks. I remember most of that stuff now. It amaze me how people can code at all with everything you need to remember or knowing what to even look up. I am sure I am writing lots of extra code in my projects because I am unaware of some method in some object that simplifies a bunch of code that I wrote.

I have to sit at my desk every night to learn and remember what I learned. If I take a few weeks off I start to unlearn the stuff.
 
You're using Xcode 4, right? Turn on the right panel and click on the wavy lines tab. While you type, it'll give you links to the documentation on whatever object or method you've just typed.

Practice helps make it all stick, though.
 
The = will never be inside of the []s.

You're right in the sense that the way lars was sticking the [] around his assignment statement was wrong. Technically it is possible to do an assignment inside of the [] in the process of sending a message, although it would be a strange thing to do:

Code:
int x = 10;
[myObject setInteger:(x = 5)];

This code will assign 5 to x AND pass 5 as the argument to setInteger, since assignment returns the value being assigned.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.