PDA

View Full Version : loading images/text to xib from plist tableview




gbenna
Jul 28, 2011, 11:48 AM
I am developing an app that starts with a grouped tableview cell. Each cell has an image, text, and description from a plist. When a cell is chosen a xib is loaded by a view controller in the plist. I want to load into the xib some text, an image, and a sound file that is in the plist (dict for that cell). This way I don't have to have lots of view controllers and xibs. I have been able to load the xib using this method but I can't get the images and text to load. I have been able to do it when I don't have a grouped table view but when I add the grouping in the plist the connection is lost. below is my code. Could someone look at it and tell me where I've gone wrong, how to correct it, or another way to do what I want to do?
I know I am not calling the right array and then dictionary but I don't know how to correct this. Help please.


// RootViewController.h
// TableViewPush
//


#import <UIKit/UIKit.h>


@interface RootViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {

NSArray *tableDataSm;
}

@property (nonatomic, retain) NSArray *tableDataSm;

@end
// RootViewController.m
// TableViewPush
//

//

#import "RootViewController.h"
#import "Location One.h"
#import "HowToUseViewController.h"
#import "TableViewPushAppDelegate.h"


@implementation RootViewController

@synthesize tableDataSm;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
const NSDictionary *const row = [self rowForIndexPath:indexPath];
NSString *wantedClassName = [row objectForKey:@"controller"];

UIViewController *const vc = [[NSClassFromString (wantedClassName) alloc] init];
NSLog(@"controller is -%@-", wantedClassName);
[self.navigationController pushViewController:vc animated:YES];

TableViewPushAppDelegate *appDelegate = ( TableViewPushAppDelegate *)[[UIApplication sharedApplication]delegate];
appDelegate.myImage = [[NSString alloc]initWithFormat:@"%@",[[tableDataSm objectAtIndex:indexPath.row]objectForKey:@"picture"]];

NSLog(@"%@", appDelegate.myImage);

appDelegate.textView = [[NSString alloc]initWithFormat:@"%@",[[tableDataSm objectAtIndex:indexPath.row]objectForKey:@"description"]];

[vc release];


}
// TableViewPushAppDelegate.h
// TableViewPush
//

//

#import <UIKit/UIKit.h>


@class RootViewController, HowToUseViewController;

@interface TableViewPushAppDelegate : UIViewController <UIApplicationDelegate> {
NSString *myImage;
NSString *textView;

UIWindow *window;

UINavigationController *navigationController;
HowToUseViewController *howToUseViewController;
}



@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet RootViewController *viewController;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@property(retain,nonatomic)NSString *myImage;
@property(retain,nonatomic)NSString *textView;

@end



// TableViewPushAppDelegate.m
// TableViewPush
//


#import "TableViewPushAppDelegate.h"
#import "RootViewController.h"


@implementation TableViewPushAppDelegate


@synthesize window;

@synthesize navigationController;
@synthesize viewController;

@synthesize myImage;
@synthesize textView;
// Location One.h
// TableViewPush
//

//

#import <UIKit/UIKit.h>
#import "RootViewController.h"

@interface Location_One: UIViewController {
IBOutlet UIImageView *imageOne;
IBOutlet UITextView *textViewTwo;
}
@property (nonatomic, retain) UITextView *textViewTwo;
@property (nonatomic, retain) UIImageView *imageOne;
@end
// Location One.m
// TableViewPush
//

//

#import "Location One.h"
#import "TableViewPushAppDelegate.h"

@implementation Location_One

@synthesize textViewTwo;
@synthesize imageOne;

-(id) init{
if((self = [super initWithNibName:@"Location One" bundle:nil])){

}
return self;
}


- (void)viewDidLoad {
NSLog(@"InView did load");

[super viewDidLoad];

TableViewPushAppDelegate *appDelegate = (TableViewPushAppDelegate *)[[UIApplication sharedApplication]delegate];


textViewTwo.text = [[NSString alloc] initWithFormat:@"%@", appDelegate.textView];

NSString *path = [[NSString alloc]initWithFormat:@"%@",appDelegate.myImage];
UIImage *img = [UIImage imageNamed:path];
[imageOne setImage:img];
}
plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>header</key>
<string>85710</string>
<key>rows</key>
<array>
<dict>
<key>text</key>
<string>52 Glass Illusions Studio</string>
<key>detailText</key>
<string>150 S Camino Seco, #119</string>
<key>image</key>
<string>VisualFEight.png</string>
<key>controller</key>
<string>Location_One</string>
<key>picture</key>
<string>VisualOne.png</string>
<key>audio</key>
<string>AudioOne.mp3</string>
<key>description</key>
<string>TextOne</string>
</dict>