我正在尝试使用文件中的UITableView
信息填充 a plist
。我不知道我哪里错了。请检查我的要求的图像和上面的代码以找出我错的地方
我需要以UITableView
数组a, b, c
应该出现在部分字段中的方式填充上面的图像。它就在下面。但我无法填充数据,请检查我错在哪里。.
.
@synthesize mySections //array
@synthesize myData //dictionary
- (void)viewDidLoad
{
[super viewDidLoad];
//LOADING THE PLIST FILE
//Create a string representing the file path
NSString *plistPath = [bundle pathForResource:@"yourFilename" ofType:@"plist"];
//Load the file in a dictionnary
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.myData = dict;
//SORTING THE DICTIONARY
NSArray *dicoArray = [[self.myData allKeys] sortedArrayUsingComparator:^(id firstObject, id secondObject) {
return [((NSString *)firstObject) compare:((NSString *)secondObject) options: NSCaseInsensitiveSearch];
}];
self.mySections = dicoArray;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//Return the number of sections.
return [self.mySections count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSString *key = [self.mySections objectAtIndex:section];
NSArray *dataInSection = [self.myData objectForKey:key];
return [dataInSection count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *key = [self.mySections objectAtIndex:section];
return [NSString stringWithFormat:@"%@", key];
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.mySections;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [self.mySections objectAtIndex:section];
NSDictionary *Dic=self.myData;
NSArray *dataForSection = [self.myData objectForKey:Dic.allKeys];
cell.textLabel.text = [dataForSection objectAtIndex:row];
return cell;
}
plist的一些代码是: -
<plist version="1.0">
<dict>
<key>a</key>
<array>
<dict>
<key>Beta_Carot_(µg)</key>
<string>0.06</string>
<key>Cholestrl_(mg)</key>
<string>215</string>
<key>fdgdfg</key>
<string>dfgdfg</string>
</dict>
</array>
<key>b</key>
<array>
<dict>
<key>Alpha_Carot_(µg)</key>
<string>0</string>
<key>gdfg</key>
<string>fdgdfg</string>
</dict>
</array>
<key>c</key>
<array>
<dict>
<key>Alpha_Carot_(µg)</key>
<string>0</string>
<key>Ash_(g)</key>
<string>0</string>
<key>Beta_Caro</key>
<string>193</string>
<key>Choline_Tot_ (mg)</key>
<string>22.3</string>
<key>dgd</key>
<string>dgdf</string>
</dict>
</array>
</dict>
</plist>