Настройка объекта UITableViewCell с уникальным изображением и текстом - PullRequest
2 голосов
/ 03 февраля 2011

Учитывая .plist, аналогичное:

<plist version="1.0">
<array>
    <dict>
        <key>name</key>
        <string>Afghanistan</string>
        <key>government</key>
        <string>Islamic Republic</string>
        <key>population</key>
        <integer>0</integer>
        <key>image</key>
        <string>/Flags/afghanistan.png</string>
    </dict>
    <dict>
        <key>name</key>
        <string>Albania</string>
        <key>government</key>
        <string>Emerging Democracy</string>
        <key>population</key>
        <integer>0</integer>
        <key>image</key>
        <string>/Flags/albania.png</string>
    </dict>
.... etc. 

И ViewController.m :

#import "FirstViewController.h"
@implementation FirstViewController
@synthesize sortedCountries;

-(void)viewDidLoad  {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"countries" ofType:@"plist"];  

    NSArray *countries = [NSArray arrayWithContentsOfFile:path];
    NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease];
    self.sortedCountries = [countries sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];


}   

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSDictionary *country = [sortedCountries objectAtIndex:indexPath.row];
        NSString *countryName = [country objectForKey:@"name"];

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier] autorelease];
    }

    NSDictionary *item = (NSDictionary *)[sortedCountries objectAtIndex:indexPath.row];
    NSString *path = [[NSBundle mainBundle] pathForResource:[item objectForKey:@"image"] ofType:@"png"];
    UIImage *theImage = [UIImage imageWithContentsOfFile:path];

    cell.imageView.image = theImage;
    cell.textLabel.text = countryName;
    return cell; 

    } 

Это ставит countryName в возрастающем алфавитном порядкеожидается в ячейках UITableView.Что не работает, так это cell.imageView.image.

. Он помещает afghanistan.png в первый UITableViewCell, как и ожидалось.Клетки, которые следуют, также получают afghanistan.png также как бы то ни было.(Вторая ячейка должна получить albania.png и т. Д.).Как я могу исправить эту проблему UIImageView?

Ответы [ 2 ]

2 голосов
/ 03 февраля 2011

Используйте imageNamed вместо.

cell.imageView.image = [UIImage imageNamed:[item objectForKey:@"image"]];
1 голос
/ 03 февраля 2011

Вы должны использовать [item objectForKey:@"image"] вместо [item objectForKey:@"imageKey"] (поскольку .plist использует <key>image</key>).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...