开发者

Accessing dictionaries in PList for UITableView

开发者 https://www.devze.com 2023-04-12 05:11 出处:网络
I\'d like to create a simple reference app that lists a group of people, their job title, and their portrait. What I have so far is the list of people and their job title. It works alright, but I thin

I'd like to create a simple reference app that lists a group of people, their job title, and their portrait. What I have so far is the list of people and their job title. It works alright, but I think I should have done it differently.

From reading other posts, I suppose I should be using dictionaries. This is how my PList currently looks:

Accessing dictionaries in PList for UITableView

And this is how the important bits of my code look:

@implementation RootViewController

@synthesize staffArray, subtitleArray;


- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString* path = [[NSBundle mainBundle] pathForResource:@"StaffData" ofType:@"plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSMutableArray *tmpNameArray = [dict objectForKey:@"Root"];
    self.staffArray = [[NSMutableArray alloc] initWithArray:tmpNameArray copyItems:YES];
    NSMutableArray* tmpSubtitleArray = [dict objectForKey:@"Subs"];
    self.subtitleArray = [[NSMutableArray alloc] initWithArray:tmpSubtitleArray copyItems:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [staffArray 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:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell.
cell.textLabel.text = [staffArray objectAtIndex:indexPath.row];开发者_JAVA百科
cell.detailTextLabel.text = [subtitleArray objectAtIndex:indexPath.row];
return cell;
}

Using two arrays kind of defeats the purpose of OOP, I think, because in this case the people aren't connected to their job titles; they just happen to be in the same order. I'd like to create for example:

Array called Jonas, first value = job title, second value = pathToImage.png.

Another array called Andreas, etc etc etc.

What do I do?


I think that as a start, your design lacks an "Employee" object, that has data members like "Name", "JobTitle", etc... After you have this set up, just create an array of people and take whatever you need from there, by index.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号