Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-12-2011, 01:59 PM   #1 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 63
CanDev is on a distinguished road
Default cell.imageview.image problem

I am loading a table with images. I am populating that table with a plist for information, and images, etc.

The problem I am having is that the cells that dont have an image assigned, are showing up with one anyway. The code I am using is this
Code:
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
            // init with style indicates cell style
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
   //adding image to cell
    NSString *path = [[NSBundle mainBundle] pathForResource:[dictionary objectForKey:@"imageKey"] ofType:@"png"];
    UIImage *theImage = [UIImage imageWithContentsOfFile:path];
    NSLog(@"key: %@", [dictionary objectForKey:@"imageKey"]);
cell.imageView.image = theImage;
    NSLog(@"image: %@", theImage);
    return cell;
}
So in my plist, I have only used the key "imageKey" in some of the entries, however, I am getting an image in all my cells
CanDev is offline   Reply With Quote
Old 10-12-2011, 02:26 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

I'm going to guess that you're still getting a path. Log that and see what happens.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-12-2011, 03:20 PM   #3 (permalink)
Registered Member
 
Join Date: Oct 2011
Location: NYC
Posts: 29
objch is on a distinguished road
Default

Also, the way you have it right now, every cell is getting the same image every time.

If you want to have different images in different cells, you need to make use of the indexPath argument of tableView:cellForRowAtIndexPath: and relate that to the contents of your property list.
objch is offline   Reply With Quote
Old 10-12-2011, 04:12 PM   #4 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 63
CanDev is on a distinguished road
Default

the way i have it now, i get the correct (different) images for cells that i put a value in for the key "imageKey".

Yes I do get a path when logged and an image for each cell, but the key returns (null) for those that dont have a key ... which is correct.

So I want to have an image show up for only those cells that I assign a value to the key
CanDev is offline   Reply With Quote
Old 10-12-2011, 04:13 PM   #5 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

You're getting a path, so presumably you're always getting an image, so you're never clearing out the image view. Change that.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-12-2011, 04:23 PM   #6 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 63
CanDev is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
You're getting a path, so presumably you're always getting an image, so you're never clearing out the image view. Change that.
how do i do that?
CanDev is offline   Reply With Quote
Old 10-12-2011, 04:24 PM   #7 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

cell.imageView.image = nil;
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-12-2011, 04:27 PM   #8 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 63
CanDev is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
cell.imageView.image = nil;
ok but where do I put that
CanDev is offline   Reply With Quote
Old 10-12-2011, 04:28 PM   #9 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Are you a programmer or not? Make some decisions. Use some programming logic. When do you want an image? When do you NOT want an image?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-12-2011, 05:12 PM   #10 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 63
CanDev is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Are you a programmer or not? Make some decisions. Use some programming logic. When do you want an image? When do you NOT want an image?
isnt that the whole point of the list. To place images where you indicate a key and the parts of the list that dont have an associated key with that value should get the image.
CanDev is offline   Reply With Quote
Old 10-12-2011, 05:13 PM   #11 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

But we've already established that you get a path every time, so your logic is flawed.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-12-2011, 05:19 PM   #12 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 63
CanDev is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
But we've already established that you get a path every time, so your logic is flawed.
exactly, and i dont know how to fix that. Thats why i ask the people who know these things. It would seem logical to me that when the program looks through the plist, it would only place an image in the table where the key would indicate and if there were no key in that dictionary then no image would be available to be shown. So I dont know where its getting the path from when that key doesnt exist.
CanDev is offline   Reply With Quote
Old 10-12-2011, 05:26 PM   #13 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

You keep missing my point. Which part of the code - given that we have proven you get a path every time - leads you to believe that you would ever NOT get an image? What are you doing to prevent there from being an image shown?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-12-2011, 05:34 PM   #14 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 63
CanDev is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
You keep missing my point. Which part of the code - given that we have proven you get a path every time - leads you to believe that you would ever NOT get an image? What are you doing to prevent there from being an image shown?
Should this
Code:
NSString *path = [[NSBundle mainBundle] pathForResource:[dictionary objectForKey:@"imageKey"] ofType:@"png"];
not limit the responses and only return a path for those items with the key "imageKey". What I dont understand and obviously what I am missing is, why do I get a path returned everytime even though there is no key with that name.

My top layer of my table has an image for each table entry, then as I drill down, some of the sub catagories do not have images assigned (ie. No key, or no value for the key "imageKey"

example snippet of my plist
Code:
					<dict>
							<key>imageKey</key>
							<string>Ukraine</string>
							<key>Title</key>
							<string>Ukraine</string>
						</dict>
						<dict>
							<key>imageKey</key>
							<string>United-Kingdom</string>
							<key>Title</key>
							<string>United Kingdom</string>
						</dict>
						<dict>
							<key>imageKey</key>
							<string>Vatican-City</string>
							<key>Title</key>
							<string>Vatican City</string>
						</dict>
					</array>
					<key>Title</key>
					<string>Europe</string>
				</dict>
				<dict>
					<key>Children</key>
					<array>
						<dict>
							<key>Title</key>
							<string>Afghanistan</string>
						</dict>
						<dict>
							<key>Title</key>
							<string>Bahrain</string>
						</dict>
						<dict>
							<key>Title</key>
							<string>Bangladesh</string>
						</dict>
						<dict>
							<key>Title</key>
							<string>Bhutan</string>
						</dict>
						<dict>
							<key>Title</key>
							<string>Brunei</string>
						</dict>
						<dict>
							<key>Title</key>
							<string>Burma</string>
						</dict>
						<dict>
							<key>Title</key>
							<string>Cambodia</string>
						</dict>
						<dict>
							<key>Title</key>
							<string>China</string>
						</dict>
but every single row of every level has an image and thats what i dont understand
CanDev is offline   Reply With Quote
Old 10-12-2011, 05:40 PM   #15 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

As objch suggested, why would the value for "dictionary" ever be different?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-12-2011, 05:43 PM   #16 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 63
CanDev is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
As objch suggested, why would the value for "dictionary" ever be different?
because its the value of "imageKey" that is different (or not there at all). Where I do have values for it, I get different images for each cell as expected.
CanDev is offline   Reply With Quote
Old 10-12-2011, 05:45 PM   #17 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

No no, the dictionary itself. Why would the dictionary ever be different? Is there some code that you aren't showing here? Because if that is the complete cellForRow above, then dictionary will be the same for every single row.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-12-2011, 05:55 PM   #18 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 63
CanDev is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
No no, the dictionary itself. Why would the dictionary ever be different? Is there some code that you aren't showing here? Because if that is the complete cellForRow above, then dictionary will be the same for every single row.


This is my cellForRowAtIndexPath

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
            // init with style indicates cell style
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
        // Set up the cell...
    NSDictionary *dictionary = [self.competingBands objectAtIndex:indexPath.row];
        // adding arrow to right side of cell
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        // main cell text
    cell.textLabel.text = [dictionary objectForKey:@"Title"];
        //use if you want to change text color for every other row
    UIColor *color = ((indexPath.row % 2) == 0) ? [UIColor colorWithRed:0/255 green:0/255 blue:0/255 alpha:1] : [UIColor blackColor];
    cell.textLabel.textColor = color;
        // subtitle cell text
    cell.detailTextLabel.text = [dictionary objectForKey:@"Description"];
    cell.detailTextLabel.textColor = [UIColor blackColor];
    cell.detailTextLabel.textAlignment = UITextAlignmentLeft;
        //adding image to cell
    NSString *path = [[NSBundle mainBundle] pathForResource:[dictionary objectForKey:@"imageKey"] ofType:@"png"];
    NSLog(@"Path: %@", path);
    UIImage *theImage = [UIImage imageWithContentsOfFile:path];
    NSLog(@"key: %@", [dictionary objectForKey:@"imageKey"]);
        // to make rounded edges on square corners
        //cell.imageView.layer.masksToBounds = YES;
        //cell.imageView.layer.cornerRadius = 5.0;
    
    cell.imageView.image = theImage;
    NSLog(@"image: %@", theImage);
    return cell;
}
and didSelectRow is as follows
Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        //Get the dictionary of the selected data source.
    NSDictionary *dictionary = [self.competingBands objectAtIndex:indexPath.row];

    //Get the children of the present item.
    NSArray *Children = [dictionary objectForKey:@"Children"];
    
    if([Children count] == 0) {
        DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
		[self.navigationController pushViewController:dvController animated:YES];
		[dvController release];
        
    }
    else {
        NSLog(@"Entering %s", __PRETTY_FUNCTION__);
            //Prepare to tableview.
        BandListViewController *BLVController = [[BandListViewController alloc] initWithNibName:@"BandList" bundle:[NSBundle mainBundle]];
        
            //Increment the Current View
        BLVController.CurrentLevel += 1;
        NSLog(@"Implement current View");
            //Set the title;
        BLVController.CurrentTitle = [dictionary objectForKey:@"Title"];
        
            //Push the new table view on the stack
        [self.navigationController pushViewController:BLVController animated:YES];
        NSLog(@"New view pushed onto stack");
        BLVController.competingBands = Children;
        
        [BLVController release];
    }
    
}
CanDev is offline   Reply With Quote
Old 10-12-2011, 06:00 PM   #19 (permalink)
Registered Member
 
Join Date: Oct 2011
Location: Montana
Posts: 30
WChambers is on a distinguished road
Default

Try something along these lines(will probably need some tweaks):

Code:
 NSString *Image = [[NSBundle mainBundle] pathForResource:[dictionary objectForKey:@"imageKey"];
    Image = [Image stringByAppendingFormat:@".png"];
    cell.imageView.image = [UIImage imageNamed:Image];
This is assuming your .png file has the same name as your value for Key. ie : imageKey = Russia, and the png is Russia.png (not RussiaImage.png).

Hope this helps/provides some insight.
WChambers is offline   Reply With Quote
Old 10-12-2011, 06:11 PM   #20 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 63
CanDev is on a distinguished road
Default

Quote:
Originally Posted by WChambers View Post
Try something along these lines(will probably need some tweaks):

Code:
 NSString *Image = [[NSBundle mainBundle] pathForResource:[dictionary objectForKey:@"imageKey"];
    Image = [Image stringByAppendingFormat:@".png"];
    cell.imageView.image = [UIImage imageNamed:Image];
This is assuming your .png file has the same name as your value for Key. ie : imageKey = Russia, and the png is Russia.png (not RussiaImage.png).

Hope this helps/provides some insight.
Sorry that didnt work, just ended up crashing. Will keep working on it and post here when I have a solution
CanDev is offline   Reply With Quote
Old 10-12-2011, 06:39 PM   #21 (permalink)
Registered Member
 
Join Date: Oct 2011
Location: Montana
Posts: 30
WChambers is on a distinguished road
Default

I'm not very experienced, so take my advice with a grain or two of salt.

I think the issue may have something to do with the lack of imageKey on some of the dictionaries. If its returning "null" ofType "png", is it assigning the first available png file from your resources? Do you have an image set manually in IB, such that when it gets null, it just has the one initially assigned?

Just throwing stuff out there to ponder. I have a new found hatred for images on cells from my own app experience these last 2 days.
WChambers is offline   Reply With Quote
Old 10-12-2011, 06:40 PM   #22 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Ah yes, when posting code, it's always useful to neglect to point out that not everything is included. That makes troubleshooting so much easier.

So in the rows that are not supposed to have images, what images do they wind up getting? Are they the same every time, or different?

And the bottom line is this: you're expecting the path to be nil in that case. That isn't happening, so you need to try something else. You've already stated two or three times what the criteria is, so I really don't understand why you're having so much trouble thinking of something to try.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-12-2011, 09:29 PM   #23 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 63
CanDev is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Ah yes, when posting code, it's always useful to neglect to point out that not everything is included. That makes troubleshooting so much easier.

So in the rows that are not supposed to have images, what images do they wind up getting? Are they the same every time, or different?

And the bottom line is this: you're expecting the path to be nil in that case. That isn't happening, so you need to try something else. You've already stated two or three times what the criteria is, so I really don't understand why you're having so much trouble thinking of something to try.
The rows that are not supposed to have images, have the first image alphabetically that i have in my images folder. So is there a way to have a key return nil instead of null. Obviously on a null return, it defaults to my first image, which I dont want.

My images are all set programatically, not thru IB
CanDev is offline   Reply With Quote
Old 10-12-2011, 10:11 PM   #24 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

You're not thinking this through at all. The problem is not with the key, the problem is with the path sniffer. objectForKey already does return nil if the key isn't there. SO TEST FOR THE PRESENCE OF THE KEY. Jesus. If you want A to happen in one situation, but B to happen in another, write a freaking test. if A else B. This is Programming Logic 101 here.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-12-2011, 10:22 PM   #25 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 63
CanDev is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
You're not thinking this through at all. The problem is not with the key, the problem is with the path sniffer. objectForKey already does return nil if the key isn't there. SO TEST FOR THE PRESENCE OF THE KEY. Jesus. If you want A to happen in one situation, but B to happen in another, write a freaking test. if A else B. This is Programming Logic 101 here.
Well first off, I never once said i was a programmer. This is all new to me and I am trying to do the best I can, and thats why i am asking questions on this forum. I didnt know this forum was for the experienced programmers. Anyway, if the key isnt there, its returning null, according to the NSLog, but I guess i dont know enough about that to be sure. So I will figure out an if statement to work if there is no key for my object for that particular part of my table.

Sorry to waste your time Brian, good day to you Off to read more of the documentation that isnt always as clear as I would like.

And While i am at it, I did read your series on tablesview, very good! very informative, waiting for the rest

Last edited by CanDev; 10-12-2011 at 10:25 PM.
CanDev is offline   Reply With Quote
Reply

Bookmarks

Tags
cell.image, imageview, tableview, tableview cell

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 402
16 members and 386 guests
13dario13, 7twenty7, buggen, eski, EvilElf, glenn_sayers, LunarMoon, morterbaher, n00b, pbart, QuantumDoja, sacha1996, Sami Gh, UMAD, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:25 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0