从同一个PLIST填充两个分组的表视图

时间:2010-07-30 13:40:05

标签: iphone xcode uitableview plist nsdictionary

1 个答案:

答案 0 :(得分:1)

首先,如果您使用pLists作为源,那么它们将不会保持任何形式的顺序,而不是pList中的顺序。解决这个问题的方法是在你的pList中有一个数组,然后有你的元素。对于我们来自plist的数据,如果你有一个填充了NSDictionarys的plist,你可能想要做这样的事情:

NSDictionary* dict = [NSDictionary initwithContentsOfFile:@"ApList.plist"];
//Get all of the elements in the dictionary
NSArray *array = [dict allKeys};
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [array count];
}    

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier;

MyIdentifier = @"Cell";

UITableViewCell *cell = (UITableViewCell *)[table dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
//Create a new dict based of the indexpath of the table cell, we need the array otherwise we wouldnt be able to get the key value for the dict.
    NSDictionary* currentDict = [NSDictionary valueForKey:[array objectAtIndex:indexPath.row]];
//Then once you;ve got the dict create a string from a String held in that dict, in this case the key for the String is LABEL.
NSString *title = [currentDict valueForKey:@"LABEL"];
    cell.textLabel.text = title;

return cell;
}

希望这有帮助