UITableView grouped and .plist

左心房为你撑大大i 提交于 2020-01-15 19:14:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!