简体   繁体   中英

How to populate Plist date to Tableview

I created a plist for tableview , but I don't know how to populate the plist in to Xcode (using objective c), nor my plist date.

<plist version="1.0">    
<dict>    
    <key>Robert Mitchall</key>    
    <array>    
        <string>8.png</string>    
        <string>Hey dude!</string>    
    </array>    
    <key>Carrie Ronald</key>    
    <array>    
        <string>7.png</string>    
        <string>Lets have a coffe!</string>    
    </array>    
    <key>Alicia Donnoven</key>    
    <array>    
        <string>10.png</string>    
        <string>Are you around?</string>    
    </array>    
    <key>Kenny Jonnes</key>    
    <array>    
        <string>6.png</string>    
        <string>About our discussion.</string>    
    </array>    
</dict>    
</plist>    

then, I add this code in my view controller.m , for populating.

@interface ViewController ()    
{    
    NSArray *content;    
}

-(NSArray *)content    
    {    
        if (!_content) {    
            _content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"]];    
        }    
        return _content;    
    }    


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section    
{        
return [self.content count];    
}    

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
 {        
    static NSString *CellIdentifier = @"Cell";        
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];    

cell.textLabel.text    = [[self.content objectAtIndex:indexPath.row] valueForKey:@"item 0"];        

cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"item 1"];    
return cell;    
}    

I did't get any output showing that I had an error.

结果

Actually, I need the main page in table view date to look like it does in this image.

首选的

Because the root node in your plist is a <dict> .

Update:

ViewController:

- (NSArray *)content {
    if (!_content) {
        _content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"]];
    }
    return _content;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.content count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text    = [[self.content objectAtIndex:indexPath.row] valueForKey:@"image"];

    cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"text"];
    return cell;
}

Property List.plist:

<plist version="1.0">
<array>
    <dict>
        <key>image</key>
        <string>8.png</string>
        <key>text</key>
        <string>Hey dude!</string>
        </dict>
    <dict>
        <key>image</key>
        <string>7.png</string>
        <key>text</key>
        <string>Lets have a coffe!</string>
    </dict>
    <dict>
        <key>image</key>
        <string>10.png</string>
        <key>text</key>
        <string>Are you around?</string>
    </dict>
    <dict>
        <key>image</key>
        <string>6.png</string>
        <key>text</key>
        <string>About our discussion.</string>
    </dict>
</array>
</plist>

Update:

As far as I understand "how to use Dictionary (code) instead of Plist for about task", i modify the code.

<plist version="1.0">    
<dict>    
    <key>Robert Mitchall</key>    
    <array>    
        <string>8.png</string>    
        <string>Hey dude!</string>    
    </array>    
    <key>Carrie Ronald</key>    
    <array>    
        <string>7.png</string>    
        <string>Lets have a coffe!</string>    
    </array>    
    <key>Alicia Donnoven</key>    
    <array>    
        <string>10.png</string>    
        <string>Are you around?</string>    
    </array>    
    <key>Kenny Jonnes</key>    
    <array>    
        <string>6.png</string>    
        <string>About our discussion.</string>    
    </array>    
</dict>    
</plist>   


- (NSDictionary *)content {
    if (!_content) {
        _content = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"]];
    }
    return _content;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.content count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = [self.content.allKeys objectAtIndex:indexPath.row];
    NSArray *detailArray = [self.content.allValues objectAtIndex:indexPath.row];

    cell.detailTextLabel.text = [detailArray objectAtIndex:1];
    return cell;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM