Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

troop231

macrumors 603
Original poster
Jan 20, 2010
5,826
561
Hello, I'm creating a tableView programatically without IB, and I cannot seem to figure out why my plist data isn't being displayed in the table.

My .h file:
Code:
#import <UIKit/UIKit.h>

@interface List : UIViewController <UITableViewDelegate,UITableViewDataSource>
{    
    NSDictionary *data;
    
    NSArray *tableDataSource;
}

- (IBAction)Done:(id)sender;

@property (nonatomic, retain) NSDictionary *data;

@property (nonatomic, retain) NSArray *tableDataSource;

@end

and my .m file:
Code:
#import "List.h"

#import "ViewController.h"

@interface List ()

@end

@implementation List

@synthesize tableDataSource, data;

- (void)viewWillAppear:(BOOL)animated
{   
    
    UITableView *_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, 1024, 704)];
    [self.view addSubview:_tableView];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"Data.plist"];
    
	NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
	self.data = tempDict;
    
    self.tableDataSource = [tempDict objectForKey:@"Rows"];
    
    [_tableView reloadData];
    
}

- (NSInteger)tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
     return [self.tableDataSource count];
}

- (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];
    }
    
    // Configure the cell...
    
    cell.textLabel.text = [tableDataSource objectAtIndex:indexPath.row];
    
    return cell;
}


- (IBAction)Done:(id)sender {
    
    ViewController *mainView = [[ViewController alloc] initWithNibName:nil bundle:nil];
    [self presentModalViewController:mainView animated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

@end

My plist looks like this:
8wBRE.png


Ideally, this is the end result I'm looking for, where each Row has 3 cells that show the Name, Email, and Phone keys.
hLKRh.png


Thank you in advance for any help you can provide.
 
Last edited:
There are many wrongs here :)

1. You have not assigned any datasource or delegate to your table view. In this case none of the table view methods will be called.

2. You usually do not put one time initialisation code in viewWillAppear:. As it is called every time the view becomes visible (such as after popping back to the view or dismissing a modal controller). I recommend you put it in viewDidLoad:

3. You are assigning a NSDictionary to UILablel.text, instead of a string here:
Code:
cell.textLabel.text = [tableDataSource objectAtIndex:indexPath.row];

4. Regarding the way you want it to be displayed, I would suggest you create a custom table view cell with 3 labels.

Now the code:

Code:
- (void)viewdidLoad
{   
    [super viewDidLoad];
    
    UITableView *_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, 1024, 704)];
    [self.view addSubview:_tableView];
    
    [_tableView setDataSource:self];
    [_tableView setDelegate:self];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"Data.plist"];
    
    NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
    self.data = tempDict;
    
    self.tableDataSource = [tempDict objectForKey:@"Rows"];
    
    [_tableView reloadData];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //return the correct number of sections here
}


- (NSInteger)tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
     return [self.tableDataSource count];
}

- (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];
    }
    
    // Configure the cell...
    
    cell.textLabel.text = [[tableDataSource objectAtIndex:indexPath.row] objectForKey:@"Name"];
    
    return cell;
}
 
There are many wrongs here :)

1. You have not assigned any datasource or delegate to your table view. In this case none of the table view methods will be called.

2. You usually do not put one time initialisation code in viewWillAppear:. As it is called every time the view becomes visible (such as after popping back to the view or dismissing a modal controller). I recommend you put it in viewDidLoad:

3. You are assigning a NSDictionary to UILablel.text, instead of a string here:
Code:
cell.textLabel.text = [tableDataSource objectAtIndex:indexPath.row];

4. Regarding the way you want it to be displayed, I would suggest you create a custom table view cell with 3 labels.

Now the code:

Code:
- (void)viewdidLoad
{   
    [super viewDidLoad];
    
    UITableView *_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, 1024, 704)];
    [self.view addSubview:_tableView];
    
    [_tableView setDataSource:self];
    [_tableView setDelegate:self];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"Data.plist"];
    
    NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
    self.data = tempDict;
    
    self.tableDataSource = [tempDict objectForKey:@"Rows"];
    
    [_tableView reloadData];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //return the correct number of sections here
}


- (NSInteger)tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
     return [self.tableDataSource count];
}

- (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];
    }
    
    // Configure the cell...
    
    cell.textLabel.text = [[tableDataSource objectAtIndex:indexPath.row] objectForKey:@"Name"];
    
    return cell;
}

Wow, I was not expecting anyone to post any code, but you have helped me greatly!

I managed to set up my three labels for the row, but I am getting this strange thing happening, when I scroll up, the row separator disappears entirely. It disappears on the simulator and on my iPad. Here is a screenshot of the problem, the arrow points to where the row separator should be. How could I fix this?

Edit: I fixed the issue.


BJ71r.png


Edit: I fixed the issue.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.