Note that there are some explanatory texts on larger screens.

plurals
  1. POLoad data from .plist to a TableView of iPhone
    primarykey
    data
    text
    <p>I'm praticing example from Beginning iPhone 4 Development:Exploring the iOS SDK. In Chapter 8 it shows how to load data from a plist into a tableview. The example was done with Interface Builder. I want to do it by code but encounter problems. Nothing shows up on screen....</p> <p>Here's the .h</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface FifthViewController : UIViewController &lt;UITableViewDataSource, UITableViewDelegate&gt; { NSDictionary *names; NSArray *keys; } @property (nonatomic, retain) NSDictionary *names; @property (nonatomic, retain) NSArray *keys; @end </code></pre> <p>Here's .m</p> <pre><code>#import "FifthViewController.h" @implementation FifthViewController @synthesize names; @synthesize keys; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 367) style:UITableViewStyleGrouped]; [table setDataSource:self]; [table setDelegate:self]; NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"]; NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; self.names = dict; [dict release]; NSArray *array = [[names allKeys] sortedArrayUsingSelector: @selector(compare:)]; self.keys = array; [self.view addSubview:table]; [table release]; } - (void)dealloc { [names release]; [keys release]; [super dealloc]; } - (void)viewDidUnload { self.names = nil; self.keys = nil; [super viewDidUnload]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } #pragma mark - #pragma mark Table View Data Source Methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [keys count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSString *key = [keys objectAtIndex:section]; NSArray *nameSection = [names objectForKey:key]; return [nameSection count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger section = [indexPath section]; NSUInteger row = [indexPath row]; NSString *key = [keys objectAtIndex:section]; NSArray *nameSection = [names objectForKey:key]; static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SectionsTableIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentifier] autorelease]; } cell.textLabel.text = [nameSection objectAtIndex:row]; return cell; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSString *key = [keys objectAtIndex:section]; return key; } - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return keys; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload