data from from plist did not load - iphone

Can anyone help me to solve the problem. I would like to develop my app using property list with nsobject. When run the app, there is no error but the data from bnm.plist did not load on uitableview.
Below is ViewController.m
#import "MCWViewController.h"
#import "MCWPlacesDetailViewController.h"
#import "MCWPlaces.h"
#interface MCWViewController ()
#end
#implementation MCWViewController {
NSArray *places;
}
#synthesize tableView = _tableView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Initialize table data
// Find out the path of bnm.plist
NSString *path = [[NSBundle mainBundle] pathForResource:#"bnm" ofType:#"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
MCWPlaces *placeGO = [MCWPlaces new];
placeGO.name = [dict objectForKey:#"PlaceName"];
placeGO.info = [dict objectForKey:#"PlaceInfo"];
places = [NSArray arrayWithObject:placeGO];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [places count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"PlacesCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSLog(#"PlacesCell");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
MCWPlaces *place = [places objectAtIndex:indexPath.row];
cell.textLabel.text = place.name;
return cell;
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"showPlaceDetails"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
MCWPlacesDetailViewController *destViewController = segue.destinationViewController;
destViewController.place = [places objectAtIndex:indexPath.row];
}
}
Below is Places.h
#import <Foundation/Foundation.h>
#interface MCWPlaces : NSObject
#property (nonatomic, strong) NSString *name;
#property (nonatomic, strong) NSString *info;
#end
Below is Places.m
#import "MCWPlaces.h"
#implementation MCWPlaces
#synthesize name;
#synthesize info;
#end
Below is Details.m
#import "MCWPlacesDetailViewController.h"
#interface MCWPlacesDetailViewController ()
#end
#implementation MCWPlacesDetailViewController
#synthesize placeInfo;
#synthesize place;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = place.name;
self.placeInfo.text = place.info;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Below is Details.h
#import <UIKit/UIKit.h>
#import "MCWPlaces.h"
#interface MCWPlacesDetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *placeInfo;
#property (nonatomic, strong) MCWPlaces *place;
#end
bnm.plist
<plist version="1.0">
<dict>
<key>PlaceName</key>
<array>
<string>Place 1</string>
<string>Place 2</string>
<string>Place 3</string>
</array>
<key>PlaceInfo</key>
<array>
<string>Place 1 Info</string>
<string>Place 2 Info</string>
<string>Place 3 Info</string>
</array>
</dict>
</plist>
Did something wrong in my code?
I think my problem is 'cellForRowAtIndexPath' and i have declared 'name' and 'info' as nsstring on Places.h. but on plist is as nsarray. but i dont know how to solve the problem.

Ok so you are in the wrong way.
Use the following code and replace it in your project :
bnm.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>PlaceName</key>
<string>Place 1</string>
<key>PlaceInfo</key>
<string>Place 1 Info</string>
</dict>
<dict>
<key>PlaceName</key>
<string>Place 2</string>
<key>PlaceInfo</key>
<string>Place 2 Info</string>
</dict>
</array>
</plist>
MCWPlaces.h
#interface MCWPlaces : NSObject
#property (nonatomic, strong) NSString *name;
#property (nonatomic, strong) NSString *info;
#end
MCWPlaces.m
#import "MCWPlaces.h"
#implementation MCWPlaces
#synthesize name;
#synthesize info;
#end
MCWViewController.m
#import "MCWViewController.h"
#import "MCWPlaces.h"
#define KEY_PLACE_NAME #"PlaceName"
#define KEY_PLACE_INFO #"PlaceInfo"
#interface MCWViewController ()
#property (nonatomic, weak) IBOutlet UITableView *tableView;
#property (nonatomic, strong) NSMutableArray *places;
#end
#implementation MCWViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Init place array
_places = [NSMutableArray array];
// Find out the path of bnm.plist
NSArray *data = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bnm" ofType:#"plist"]];
NSLog(#"%#", data);
for (NSInteger i = 0; i < data.count; i++)
{
MCWPlaces *placeGO = [MCWPlaces new];
placeGO.name = [[data objectAtIndex:i] objectForKey:KEY_PLACE_NAME];
placeGO.info = [[data objectAtIndex:i] objectForKey:KEY_PLACE_INFO];
[_places addObject:placeGO];
}
NSLog(#"%#", _places);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _places.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"PlacesCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSLog(#"PlacesCell");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSLog(#"%#", [_places objectAtIndex:indexPath.row]);
cell.textLabel.text = [[_places objectAtIndex:indexPath.row] name];
return cell;
}
#end

I think you have a problem when you load your data.
Firstly you say you get data from place.plist but in your viewDidLoad you tried to load data from bnm.plist
Try to use the following code :
- (void)viewDidLoad
{
[super viewDidLoad];
// Initialize table data
// Find out the path of bnm.plist
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"bnm" ofType:#"plist"]];
// Load the file content and read the data into arrays
MCWPlaces *placeGO = [MCWPlaces new];
placeGO.name = [dict objectForKey:#"PlaceName"];
placeGO.info = [dict objectForKey:#"PlaceInfo"];
}
Could you show us your bnm.plist ?

Related

Get Address Book data it's code run on simulator but didn't run iPhone Device.

I have got a address book data into the tableView in my application.This code is running in simulator successfully. i got the data of contacts in the tableView of my app in simulator but when i test this in my device then data of contacts didn't get. I used 6.1 simulator and iPhone device ios 6.1.3. Please suggest me what is the actual problem in this code ?
This code is run in simulator successfully but it's code didn't run in iPhone device iOS 6.1.3
Thanks You
ClsMainPageAppDelegate.h
#import <UIKit/UIKit.h>
#import "Person.h"
#interface ClsMainPageAppDelegate : UIResponder <UIApplicationDelegate>
{
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) Person *objPerson;
#end
ClsMainPageAppDelegate.m
#import "ClsMainPageAppDelegate.h"
#import "ClsMainPageViewController.h"
#implementation ClsMainPageAppDelegate
#synthesize objPerson;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
}
Person.h
#import <Foundation/Foundation.h>
#interface Person : NSObject
#property (nonatomic, strong) NSString *firstName;
#property (nonatomic, strong) NSString *lastName;
#property (nonatomic, strong) NSString *fullName;
#property (nonatomic, strong) NSString *phoneNumber;
#property (nonatomic, strong) NSString *workEmail;
#end
Person.m
#import "Person.h"
#implementation Person
- (id)init
{
self = [super init];
if (self)
{
}
return self;
}
#end
ClsAddressBookViewController.h
#import <UIKit/UIKit.h>
#interface ClsAddressBookViewController : UIViewController
- (IBAction)backcontacts:(id)sender;
#end
ClsAddressBookViewController.m
#import "ClsAddressBookViewController.h"
#import "ClsUpdateNetworkViewController.h"
#import "ClsMainPageAppDelegate.h"
#import "Person.h"
#import <AddressBook/AddressBook.h>
#interface ClsAddressBookViewController () <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) NSMutableArray *tableData;
#end
#implementation ClsAddressBookViewController
#synthesize tableData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = #"Contacts";
self.tableData = [[NSMutableArray alloc]init];
[self getPersonOutOfAddressBook];
}
- (void)getPersonOutOfAddressBook
{
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBook != nil)
{
NSLog(#"Succesful.");
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i = 0;
for (i = 0; i < [allContacts count]; i++)
{
Person *person = [[Person alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
NSString *fullName = [NSString stringWithFormat:#"%# %#", firstName, lastName];
person.firstName = firstName;
person.lastName = lastName;
person.fullName = fullName;
//Mobile Number
ABMultiValueRef mobilenum = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
NSUInteger k = 0;
for(k = 0; k < ABMultiValueGetCount(mobilenum); k++)
{
NSString *mobile = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(mobilenum, k);
if(k == 0)
{
person.phoneNumber = mobile;
NSLog(#"person.mobilenum = %#", person.phoneNumber);
}
else if (k==1)
person.phoneNumber = mobile;
}
[self.tableData addObject:person];
}
}
CFRelease(addressBook);
}
#pragma mark TableView Delegate
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.tableData count];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
Person *person = [self.tableData objectAtIndex:indexPath.row];
cell.textLabel.text = person.fullName;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Person *person = [self.tableData objectAtIndex:indexPath.row];
ClsMainPageAppDelegate *objdelegate = [[UIApplication sharedApplication]delegate];
objdelegate.objPerson = person;
UIStoryboard *storybrd = self.storyboard;
ClsAddressBookViewController *svc = [storybrd instantiateViewControllerWithIdentifier:#"clsUpdatecontrol"];
[self presentViewController:svc animated:YES completion:nil ];
}
From IOS 6. it need to authorize your apps for to access system datas :
In settings on your IPhone in confidentiality tab and contact section, verify if your app has acces is "on"
To continue TDelepine's suggestion, perhaps in your getPersonOutOfAddressBook method, you can check if your app has AddressBook access. The code below will force the device to ask the user for access if access status has not been determined:
...
NSLog(#"Succesful.");
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if(granted) {
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
etc...
}
});
}
I think you're not sending request for access Address Book. One other thing If you're not using this then It's work properly in simulator but not working in iPhone
Use this code in ViewDidLoad
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
// dispatch_release(sema);
}
else { // we're on iOS 5 or older
accessGranted = YES;
}
if (accessGranted) {
// Do whatever you want here.
}

How to copy an array to appdelegate array and then retrieve it again elsewhere?

I want to store the array of urls in my appdelegate array i.e. logoArray from myMutableArray and then use it in other viewcontroller, but i am uable to copy as may be i am doing shallow copy, i have tried othes ways also like initwithArray:copyItems.
code:-
#class FirstViewController;
#interface AppDelegate_iPhone : NSObject <UIApplicationDelegate> {
UIWindow *window;
FirstViewController *viewController;
NSMutableArray *logoArray;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) NSMutableArray *logoArray;
#end
// NO initialization of logoArra is done in .M file
#class AppDelegate_iPhone;
#interface FirstViewController : UIViewController {
NSMutableArray *array;
NSString *logoString;
AppDelegate_iPhone *appDelegate;
}
#end
#implementation FirstViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
int x=5,y=10;
UIApplication *app = [UIApplication sharedApplication];
appDelegate=app.delegate;
NSLog(#" Array ====== %d",[appDelegate.logoArray count]);
array = [[NSMutableArray alloc]initWithArray:appDelegate.logoArray];
NSLog(#"array at 0 ===== %#",[array objectAtIndex:0]);
for (int i=0; i<[array count]; i++) {
logoString = [array objectAtIndex:i];
NSLog(#"%#",logoString);
UIImage *imageFromUrl = [UIImage imageWithContentsOfFile:[NSURL fileURLWithPath:logoString]];
UIImageView *imgView = [[UIImageView alloc] initWithImage:imageFromUrl];
[imgView setFrame:CGRectMake(x, y, 196, 90)];
[self.view addSubview:imgView];
// UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onTapImage)];
// [imgView addGestureRecognizer:tgr];
// [tgr release];
//Do the rest of your operations here, don't forget to release the UIImageView
x = x + 200;
// [imgView release];
}
}
#class Litofinter,AppDelegate_iPhone;
#interface ParsingViewController : NSObject<NSXMLParserDelegate> {
NSString *myString;
NSMutableArray *myMutableArray;
Litofinter *obj;
NSString *currentElement;
AppDelegate_iPhone *appDelegate;
}
#property(nonatomic, retain) NSString *myString;
#property(nonatomic, retain) NSArray *myMutableArray;
#end
#import "ParsingViewController.h"
#import "Litofinter.h"
#import "AppDelegate_iPhone.h"
#implementation ParsingViewController
#synthesize myMutableArray, myString;
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
myMutableArray = [[NSMutableArray alloc]init];
}
// I have parsed here my XML and array gets stored in myMutableArray
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
UIApplication *app = [UIApplication sharedApplication];
appDelegate=app.delegate;
appDelegate.logoArray = [[NSMutableArray alloc]initWithArray:myMutableArray];
// NSLog(#"appDelegate.logoArray count %d",[appDelegate.logoArray count]);
for (Litofinter *lito in appDelegate.logoArray) {
NSLog(#"Array Elements :----- %#",lito.cLogo);
}
}
Personally I wouldn't create an array in a viewcontroller and then store it in the appdelegate. I'd be more inclined to create a model for the data ( a class that gets and stores the data and provides it to the view controllers).
this thread may help:
iPhone: Using a NSMutableArry in the AppDelegate as a Global Variable

Navigation application with UIWebView and UITableView

I'm trying to create an application with a UITableView and when selection a row going to another view that containing a UIWebView that will load some website(passing in the row). but i still got this log :
Unknown scheme, doing nothing:
My code is below :
// RootViewController.h
#import <UIKit/UIKit.h>
#interface RootViewController : UITableViewController {
NSArray *books;
NSArray *sites;
NSArray *contacts;
}
// RootViewController.m
#import "RootViewController.h"
#import "SitesViewController.h"
#implementation RootViewController
- (void)viewDidLoad
{
// Setting the main screen title
self.title = #"Main App Screen";
// Retreiving data from the plist file
NSString *file = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
NSDictionary *dict = [[[NSDictionary alloc] initWithContentsOfFile:file] autorelease];
sites = [[NSArray alloc] initWithArray:[dict objectForKey:#"Sites"]];
contacts = [[NSArray alloc] initWithArray:[dict objectForKey:#"Contacts"]];
books = [[NSArray alloc] initWithArray:[dict objectForKey:#"Books"]];
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// We have 3 sections
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Returning the number of rows
return numberOfrows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Returning each cell
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// section title
return title;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Handling sites section
if (indexPath.section == 0) {
SitesViewController *sitesViewController = [[SitesViewController alloc] initWithNibName:#"SitesViewController" bundle:nil];
// We want to load some data here
sitesViewController.websiteUrl = [sites objectAtIndex:indexPath.row];
// Load this view
[self.navigationController pushViewController:sitesViewController animated:YES];
// Release it
[sitesViewController release];
}
}
// SitesViewController.h
#import <UIKit/UIKit.h>
#interface SitesViewController : UIViewController {
IBOutlet UIWebView *website;
NSString *websiteUrl;
}
#property (nonatomic, retain) IBOutlet UIWebView *website;
#property (nonatomic, retain) NSString *websiteUrl;
#end
// SitesViewController.m
#import "SitesViewController.h"
#implementation SitesViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//[website setDelegate:self];
// Do any additional setup after loading the view from its nib.
NSURL *url = [[NSURL alloc] initWithString:websiteUrl];
[website loadRequest:[NSURLRequest requestWithURL:url]];
[url release];
}
....
#end
As requested the Data.plist file contain this data :
<?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">
<dict>
<key>Sites</key>
<array>
<string>www.freebsd.com</string>
<string>www.kernel.org</string>
<string>www.developer.apple.com</string>
<string>www.github.com</string>
</array>
<key>Contacts</key>
<array>
<string>user#apple.com</string>
<string>user#kernel.org</string>
<string>user#gmail.com</string>
</array>
<key>Books</key>
<array>
<string>book1</string>
<string>book2</string>
<string>book3</string>
</array>
</dict>
</plist>
Thanks for the help.
You will need to add http:// to those urls. They aren't automatically prepended with it.

Why will my plist not populate my UITableView?

I have made some progress and am editing the question to be current again. My big issue now is that the grouped table view will load, but shows all of everything in each section with each row. Also, my UIAlertView is returning all references to my plist (null). How would I fix this because the references to the plist are killing me. Any and all help would be greatly appreciated!
Here is the plain text version of my plist file:
<array>
<dict>
<key>eventName</key>
<string>Comedy Caravan</string>
<key>eventSpecifics</key>
<string>Nicholas Anthony</string>
<key>eventLocation</key>
<string>The Cats Den</string>
<key>eventType</key>
<string>Comedy</string>
<key>eventGoodies</key>
<string>Both</string>
<key>eventDate</key>
<date>2010-07-23T00:00:00Z</date>
</dict>
<dict>
<key>eventName</key>
<string>Comedy Caravan</string>
<key>eventLocation</key>
<string>The Cats Den</string>
<key>eventType</key>
<string>Comedy</string>
<key>eventSpecifics</key>
<string>Bruce Baum</string>
<key>eventGoodies</key>
<string>Prizes</string>
<key>eventDate</key>
<date>2010-07-24T00:00:00Z</date>
</dict>
<dict>
<key>eventName</key>
<string>Late Night Film Series</string>
<key>eventLocation</key>
<string>The Cats Den</string>
<key>eventType</key>
<string>Comedy</string>
<key>eventSpecifics</key>
<string>Avatar</string>
<key>eventGoodies</key>
<string>Food</string>
<key>eventDate</key>
<date>2010-07-24T02:00:00Z</date>
</dict>
</array>
Here is my ThisWeekViewController.h:
#import <UIKit/UIKit.h>
#class Event;
#interface ThisWeekViewController : UITableViewController
{
NSArray *eventNames;
Event *event;
}
#property (nonatomic, retain) NSArray *eventNames;
#property (nonatomic, retain) Event *event;
#end
and Here is my ThisWeekViewController.m:
#import "ThisWeekViewController.h"
#import "Event.h"
#implementation ThisWeekViewController
#synthesize eventNames;
#synthesize event;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Events" ofType:#"plist"];
NSArray *dict = [[NSArray alloc] initWithContentsOfFile:path];
NSArray *namesArray = [dict valueForKey:#"eventName"];
self.eventNames = namesArray;
[dict release];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
self.eventNames = nil;
}
#pragma mark Table View Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.eventNames count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *key = [NSString stringWithFormat:#"%#", event.eventName];
return key;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.eventNames count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
// Set the cell's text to the event name
cell.textLabel.text = event.eventName;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)dealloc {
// [thisWeek release];
[event release];
[eventNames release];
[super dealloc];
}
#end
I have also included my Event.h file
#import <Foundation/Foundation.h>
#interface Event : NSObject {
NSString *eventName;
NSString *eventType;
NSDate *eventDate;
NSString *eventLocation;
NSString *eventGoodies;
NSString *eventSpecifics;
}
- (id)initWithDictionary:(NSDictionary *)aDictionary;
#property (nonatomic, retain) NSString *eventName;
#property (nonatomic, retain) NSString *eventType;
#property (nonatomic, retain) NSString *eventLocation;
#property (nonatomic, retain) NSDate *eventDate;
#property (nonatomic, retain) NSString *eventGoodies;
#property (nonatomic, retain) NSString *eventSpecifics;
#end
and my Event.m file
#import "Event.h"
#implementation Event
#synthesize eventName;
#synthesize eventType;
#synthesize eventDate;
#synthesize eventLocation;
#synthesize eventGoodies;
#synthesize eventSpecifics;
- (id)initWithDictionary:(NSDictionary *)aDictionary {
if ([self init]) {
self.eventName = [aDictionary valueForKey:#"eventName"];
self.eventType = [aDictionary valueForKey:#"eventType"];
self.eventDate = [aDictionary valueForKey:#"eventDate"];
self.eventLocation = [aDictionary valueForKey:#"eventLocation"];
self.eventGoodies = [aDictionary valueForKey:#"eventGoodies"];
self.eventSpecifics = [aDictionary valueForKey:#"eventSpecifics"];
}
return self;
}
- (void)dealloc {
[eventName release];
[eventType release];
[eventDate release];
[eventLocation release];
[eventGoodies release];
[eventSpecifics release];
[super dealloc];
}
#end
I would like to be able to place things such as cell.detailTextLabel.text = event.eventSpecifics and just run off of references like that.
If I recall correctly, UITableViewController reloads the table view in -viewWillAppear:. This may very well be called before -viewDidLoad is called. I would recommend that you insert a call to [self.tableView reloadData] at the end of your implementation of -viewDidLoad.
You should also be calling [super viewDidLoad] at the top of your implementation of -viewDidLoad, and similarly for -viewDidUnload.
Also, you don't need to declare your class as conforming to UITableViewDataSource or UITableViewDelegate as your superclass (UITableViewController) already does that for you.

Problems trying to override methods in Objective-C (iPhone)

this my problem i have a class X that inherits UITableViewController class and a class Y that inherits the X class, when i try to override a method in the Y class the method in the X class is invoked... and i can't find references to understand what's happening... can anyone help me?
Thanks in advance!
Code!
mluListBuilder.h
#import <UIKit/UIKit.h>
#interface mluListBuilder : UITableViewController {
NSString *sListTitle;
NSString *sEntityName;
NSArray *aEntityProperties;
NSMutableArray *maListRecords;
NSManagedObjectContext *mocList;
NSFetchRequest *frListRecords;
NSEntityDescription *edListRecords;
NSArray *aOrderByProperties;
NSArray *aToolBarItems;
NSArray *aToolBarItemsActions;
}
#property (nonatomic, retain) NSString *sListTitle;
#property (nonatomic, retain) NSString *sEntityName;
#property (nonatomic, retain) NSArray *aEntityProperties;
#property (nonatomic, retain) NSMutableArray *maListRecords;
#property (nonatomic, retain) NSManagedObjectContext *mocList;
#property (nonatomic, retain) NSFetchRequest *frListRecords;
#property (nonatomic, retain) NSEntityDescription *edListRecords;
#property (nonatomic, retain) NSArray *aOrderByProperties;
#property (nonatomic, retain) NSArray *aToolBarItems;
#property (nonatomic, retain) NSArray *aToolBarItemsActions;
- (id) initWithStyle: (UITableViewStyle) style
listTitle: (NSString *) psListTitle
entityName: (NSString *) psEntityName
entityProperties: (NSArray *) paEntityProperties
orderListByProperties: (NSArray *) paOrderByProperties
toolBarItems: (NSArray *) paToolBarItems
toolBarItemsActions: (NSArray *) paToolBarItemsActions;
- (void)newRecord;
- (void)deleteRecord;
#end
mluListBuilder.m
#import "mluListBuilder.h"
#implementation mluListBuilder
#synthesize sListTitle,
sEntityName,
aEntityProperties,
maListRecords,
mocList,
frListRecords,
edListRecords,
aOrderByProperties,
aToolBarItems,
aToolBarItemsActions;
- (id) initWithStyle: (UITableViewStyle) style
listTitle: (NSString *) psListTitle
entityName: (NSString *) psEntityName
entityProperties: (NSArray *) paEntityProperties
orderListByProperties: (NSArray *) paOrderByProperties
toolBarItems: (NSArray *) paToolBarItems
toolBarItemsActions: (NSArray *) paToolBarItemsActions
{
sListTitle = psListTitle;
sEntityName = psEntityName;
aEntityProperties = paEntityProperties;
aOrderByProperties = paOrderByProperties;
aToolBarItems = paToolBarItems;
aToolBarItemsActions = paToolBarItemsActions;
if (self = [super initWithStyle:style]) {
}
return self;
}
- (void)viewDidLoad {
self.title = NSLocalizedString(sListTitle, nil);
if ([aToolBarItems count] > 0) {
NSMutableArray *maToolBarItems = [[NSMutableArray alloc] init];
self.navigationController.toolbarHidden = NO;
for (int i = 0; i < [aToolBarItems count]; i++) {
UIBarButtonItem * bbiToolBarItem = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString([aToolBarItems objectAtIndex:i], nil)
style:UIBarButtonItemStyleBordered
target:self
action:NSSelectorFromString([aToolBarItemsActions objectAtIndex:i])
];
[maToolBarItems addObject:bbiToolBarItem];
}
self.toolbarItems = maToolBarItems;
} else {
self.navigationController.toolbarHidden = YES;
}
if (mocList != nil) {
frListRecords = [[NSFetchRequest alloc] init];
NSSortDescriptor *sdListRecords = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
[frListRecords setSortDescriptors:[[NSArray alloc] initWithObjects:sdListRecords, nil]];
edListRecords = [NSEntityDescription entityForName:sEntityName inManagedObjectContext:mocList];
[frListRecords setEntity:edListRecords];
NSError *errFetchRequest;
maListRecords = [[mocList executeFetchRequest:frListRecords error:&errFetchRequest] mutableCopy];
}
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
NSError *errFetchRequest;
maListRecords = [[mocList executeFetchRequest:frListRecords error:&errFetchRequest] mutableCopy];
[self.tableView reloadData];
if (self.navigationController.toolbarHidden == YES) {
if ([aToolBarItems count] > 0) {
self.navigationController.toolbarHidden = NO;
}
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [maListRecords count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
for (UIView *vwExisting in cell.contentView.subviews) {
[vwExisting removeFromSuperview];
}
NSEntityDescription *edCurrentRecord = [maListRecords objectAtIndex:indexPath.row];
UILabel *lblCell = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, 280, 20.0)];
[lblCell setText:edCurrentRecord.name];
[cell.contentView addSubview:lblCell];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:#"AnotherView" bundle:nil];
// [self.navigationController pushViewController:anotherViewController];
// [anotherViewController release];
}
- (void)dealloc {
[super dealloc];
}
- (void)newRecord {
NSLog(#"%#", [self class]);
}
- (void)deleteRecord {
}
#end
mluLawyerCaseSituationsList.h
#import <Foundation/Foundation.h>
#import "mluListBuilder.h";
#interface mluLawyerCaseSituationsList : mluListBuilder {
}
- (void)newRecord;
#end
mluLawyerCaseSituationsList.m
#import "mluLawyerCaseSituationsList.h"
#implementation mluLawyerCaseSituationsList
- (void)newRecord {
NSLog(#"%#", [self class]);
}
#end
Calling the mluLawyerCaseSituationsList
mluLawyerCaseSituationsList *vcCaseSituations = [[mluListBuilder alloc]
initWithStyle:UITableViewStylePlain
listTitle:#"titCaseSituations"
entityName:#"case_situations"
entityProperties:[[NSArray alloc] initWithObjects:#"name", nil]
orderListByProperties:[[NSArray alloc] initWithObjects:#"name", nil]
toolBarItems:[[NSArray alloc] initWithObjects:#"btNew", nil]
toolBarItemsActions:[[NSArray alloc] initWithObjects:#"newRecord", nil]
];
Output... :(
2009-12-17 17:30:02.726 mluLawyer[2862:20b] mluListBuilder
Hope it helps...
I’ve been looking through your code only briefly, but it seems obvious (from code and from the output) that you allocate an instance of class X (mluListBuilder).
Of course, you cannot expect to have a method of class Y (mluLawyerCaseSituationsList), performed when Y is derived from X and the object is of class X.
So, you have:
#interface X : UITableViewController
- (void) method;
#end
#interface Y : X
- (void) method;
#end
You are calling -method, but it is being invoked on X, not Y? Only way that can happen is if you have an instance of X instead of Y (or if someone is playing very silly buggers with the runtime -- unlikely).
Add NSLog(#"%#", [self class]); to the method implementations and see what the class of the instance really is!
You don't give us much information in your question, but the following is how it should work:
Class_X.h:
#interface Class_X : UITableViewController
{
}
- (void)someMethod;
#end
Class_X.m:
#import "Class_X.h"
#implementation Class_X
- (void)someMethod
{
NSLog(#"method in Class_X was called");
}
#end
Class_Y.h:
#import "Class_X.h"
#interface Class_Y : Class_X
{
}
- (void)someMethod;
#end
Class_Y.m:
#import "Class_Y.h"
#implementation Class_Y
- (void)someMethod
{
NSLog(#"method in Class_Y was called");
}
#end
Elsewhere:
#import "Class_Y.h"
...
Class_X * x_instance = [[Class_X alloc] init];
Class_Y * y_instance = [[Class_Y alloc] init];
[x_instance someMethod];
[y_instance someMethod];
[Class_Y release];
[Class_X release];
Output:
method in Class_X was called
method in Class_Y was called