问题
I wanted to make a sectioned UITableView using plist. Originally, my plist was structured this way:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>name</key>
<string>First</string>
<key>description</key>
<string>First</string>
<key>image</key>
<string>image.png</string>
</dict>
<dict>
<key>name</key>
<string>Second</string>
<key>description</key>
<string>SecondR</string>
<key>image</key>
<string>image.png</string>
</dict>
//etc
</array>
</plist>
Where name was the name of the rows and of the rowNamed label in my detailView, description was for the string in my ´detailViewand ìmagethe UIImageView in the detailView.
The cell.textLabel.text did show the name key this way:
cell.textLabel.text = [[sortedList objectAtIndex:indexPath.row]objectForKey:@"name"];
Now,following the answer in this question, sectioned UITableView sourced from a pList, I created my new plist file with section, structured this way:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>Title</key>
<string> Section1</string>
<key>Rows</key>
<array>
<dict>
<key>name</key>
<string>Test</string>
<key>description</key>
<string>test</string>
<key>image</key>
<string>testtttt</string>
</dict>
</array>
</dict>
<dict>
<key>Title</key>
<string> Section2</string>
<key>Rows</key>
<array>
<dict>
<key>name</key>
<string>Test</string>
<key>description</key>
<string>test</string>
<key>image</key>
<string>testtttt</string>
</dict>
</array>
</dict>
</array>
</plist>
The point is that now i don't know how to make it similar to the previous plist structure (i mean, image,name and description), and i don't know how to make cellForRowAtIndexPath show the name key contained in Rows as title of the cells. I tried with
cell.textLabel.text = [[[sortedList objectAtIndex: indexPath.section] objectForKey: @"name"]objectAtIndex:indexPath.row];
But i get an empty cell.
I want cell.textLabel.text to show the key name for each element (row) contained in each section
Can someone help me?
回答1:
Your plist is an array of dictionaries. Each dictionary represents a section.
And rows within a section is present as an array whose key is Rows in that section/dictionary.
So to get a row in a particular section, first get the section. Then get the row using objectForKey:@"Rows".
Once you have rows array, it again contains the several rows as a dictionary.
NSDictionary *section = [sortedList objectAtIndex:indexPath.section];
NSArray *rows = [section objectForKey:@"Rows"];
cell.textLabel.text = [[rows objectAtIndex:indexPath.row] objectForKey:@"name"];
来源:https://stackoverflow.com/questions/10963845/uitableview-grouped-and-plist