[英]Plist to UITableView
I am trying to populate a UITableView
with information from a plist
file. 我试图用来自plist
文件的信息填充UITableView
。 I don't know where I am going wrong. 我不知道我要去哪里错了。 Please check the images for my requirement and the code above to find where I am wrong 请检查图片以了解我的要求和上面的代码,以查找错误的地方
I need to populate above image in UITableView
in such way that array a, b, c
should come in the section field. 我需要以这样的方式在UITableView
中填充上面的图像,即数组a, b, c
应该出现在section字段中。 Its coming right in below. 它就在下面。 But I am unable to populate data please check where I am wrong. 但是我无法填充数据,请检查我错了。 . 。
. 。
@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;
}
some code of plist is:- 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>
You need to get to count the number of elements in the right data structure when determining the number of rows in each section. 确定每个节中的行数时,需要计算正确数据结构中的元素数。 So in - tableView: numberOfRowsInSection:
you need to have 所以在- tableView: numberOfRowsInSection:
您需要
NSArray *dataInSection = [[self.myData objectForKey:key] objectAtIndex:0];
return [dataInSection count];
There is also a problem in - tableView: cellForRowAtIndexPath:
. - tableView: cellForRowAtIndexPath:
还有一个问题- tableView: cellForRowAtIndexPath:
You create the correct key to get the data for your section in *key
but then you instead use Dic.allKeys
as a key to retrieve data from self.myData
. 您创建了正确的密钥以在*key
获取您的部分的数据,但随后您将Dic.allKeys
用作从self.myData
检索数据的self.myData
。 So you are sending in a whole array as the key when you set *dataForSection
. 因此,当您设置*dataForSection
时,您将发送整个数组作为键。
So change these lines: 因此,更改以下行:
NSString *key = [self.mySections objectAtIndex:section];
NSDictionary *Dic=self.myData;
NSArray *dataForSection = [self.myData objectForKey:Dic.allKeys];
cell.textLabel.text = [dataForSection objectAtIndex:row];
To: 至:
NSString *key = [self.mySections objectAtIndex:section];
NSDictionary *dataForSection = [[self.myData objectForKey:key] objectAtIndex:0];
cell.textLabel.text = [[dataForSection allKeys] objectAtIndex:row];
I think you're not drilling far enough down your data hierarchy. 我认为您对数据层次结构的钻探不够深入。 In numberOfRowsInSection:
you're getting the value for the key in the root level dictionary: Looks like this is always an NSArray
with only a single item: A dictionary. 在numberOfRowsInSection:
您正在根级字典中获取键的值:看起来这始终是一个只有单个项目的NSArray
:字典。 You probably want something like 您可能想要类似
...
NSDictionary *dataInSection = [[self.myData objectForKey:key] objectAtIndex:0];
return [dataInSection count];
...
According to me you are doing some mistake in table fetch data from plist plz try this - (void)viewDidLoad { [super viewDidLoad]; 根据我的说法,您在从plist plz获取表数据时犯了一些错误,请尝试以下操作-(void)viewDidLoad {[super viewDidLoad];
//LOADING THE PLIST FILE
//Create a string representing the file path
NSString *plistPath = [bundle pathForResource:@"yourFilename" ofType:@"plist"];
NSMutableArray *listArray=[NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yourFilename" ofType:@"plist"]];
self.sections = [[NSMutableDictionary alloc] init];
}
Then set your data according to your need from plist
data come as an array, then dict
may be help this information you.. 然后根据需要从plist
数据(以数组形式)中设置数据,然后dict
可能会对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.