UIImageView inside UITableView cell (Image filename from XML)

Discussion in 'iOS Programming' started by wakka092, Feb 3, 2009.

  1. wakka092 macrumors 6502

    wakka092

    Joined:
    Jun 20, 2007
    #1
    Hello there!

    Basically, my app displays the title, summary, and the image of the formula. It displays a large amount of information parsed from a local XML file. The XML looks like this:

    Code:
    <Rule id="1">
    <title>Pythagorean Theorem</title>
    <summary>Use to find lengths of legs or the hypotenuse of a right triangle</summary>
    <image>pythagtheo.png</image>
    </Rule>
    
    It is parsed into the following in Rule.h:

    Code:
    @interface Rule : NSObject {
    	NSInteger ruleID;
    	NSString *title;
    	NSString *summary;
    	NSString *image;
    }
    
    Then everything gets displayed into a UITableViewCell :

    Code:
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 2;
    }
    
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 1;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell= [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        }
    	
    	switch(indexPath.section)
    	{
    		case 0:
    			cell.text = @"";
    			CGRect summaryRect = CGRectMake(3.0, 3.0, 290.0, 190.0);
    			UITextView *summaryView = [[UITextView alloc] initWithFrame:summaryRect];
    			summaryView.text = aRule.summary;
    			summaryView.font = [UIFont systemFontOfSize: 15.0];
    			summaryView.editable = NO;
    			[cell.contentView addSubview:summaryView];
    			[summaryView release];
    			break;
    		case 1:
    			cell.text = @"";
    			CGRect formulaRect = CGRectMake(10.0, 10.0, 290.0, 190.0);
    			UIImage *formulaImage = [UIImage imageNamed:@"sif.png"];
    			UIImageView *formulaView = [[[UIImageView alloc] initWithFrame:formulaRect] initWithImage:formulaImage];
    			[cell.contentView addSubview:formulaView];
    			[formulaView release];
    			[formulaImage release];
    			break;
    	}
    	
    	
    	return cell;
    }
    
    - (NSString *)tableView:(UITableView *)tblView titleForHeaderInSection:(NSInteger)section {
    	
    	NSString *sectionName = nil;
    	
    	
    	switch(section)
    	{
    		case 0:
    			sectionName = [NSString stringWithString:@"Summary"];
    			break;
    		case 1:
    			sectionName = [NSString stringWithString:@"Formula"];
    			break;
    	}
    	
    	return sectionName;
    }
    
    Everything's dandy with the text. It works out great; summaryView.text = aRule.summary. But what I want to do is display an image from the app's bundle based upon the contents of the <image> tag from a <rule> from the XML file.

    I need some way to take the contents of the string aRule.image and get it into a UIImageView or UIImage so I can display the formula. I've tried things like initWithData: but I've gotten nowhere.

    Any help would be very appreciated!
     
  2. wakka092 thread starter macrumors 6502

    wakka092

    Joined:
    Jun 20, 2007
    #3
  3. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #4
  4. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #5
    No, it's defined as:

    Code:
    + (UIImage *)imageNamed:(NSString *)name
    
    so it does expect an NSString*

    So does this table have just one row per section? And so just one rule per table?

    If so, aRule.image should work. Check to see that you have the correct string in there.

    Also, btw, you shouldn't be hitting two different init methods on an object like you are doing with your UIImageView.
     
  5. wakka092 thread starter macrumors 6502

    wakka092

    Joined:
    Jun 20, 2007
    #6
    Hey all. Thanks for the help. You guys have been great!

    Basically I ripped out the UIImageView and simply added the image property to the cell. I also changed aRule.image to aRule.imagename.

    Code:
    case 1:
    	cell.text = @"";
            cell.image = [UIImage imageNamed:(NSString *)aRule.imagename];
    	break;
    
    There are no errors or warnings. But when I load it into the simulator, there's no background image. The imagename tag is image.png. The imagename tag is parsed, I see it in the log. I have only one aRule, and only one row per section..

    But it still doesn't work. When I replace (NSString *)aRule.imagename with, say, @"image.png", it loads into the cell. :confused:
     
  6. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #7
    I don't think the image in a standard UITableViewCell is a background image. It's actually just an image to the left of the text.

    But anyway, I assume no image is showing at all. So why did you include the cast (NSString *). Were you getting compiler warning without it? If so, that would be a clue that aRule.imagename is not a pointer to an NSString.
     
  7. wakka092 thread starter macrumors 6502

    wakka092

    Joined:
    Jun 20, 2007
    #8
    Well, if I use aRule.imagename for UITextView's text I see the image's name - image.png. There must be a problem with the UIImage not wanting to accept the string?
     
  8. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #9
    Well, [UIImage imagenamed:] definitely wants an NSString*. You proved that by substituting the static string @"image.png"

    I can't think of anything else to tell you without seeing the project. If @"image.png" works, then you have the right pic in the bundle and all of that. And if aRule.imagename really is @"image.png", then I don't see why it wouldn't work.

    There has to be something else going on.
     
  9. sandeep123 macrumors newbie

    Joined:
    Feb 4, 2009

Share This Page