简体   繁体   中英

no images showing using a plist to populate in UITableview custom cell

I am trying to populate 2 images in a UITableView with information from a plist file.

Currently all of my label fields are populating however I am getting no images are showing. As I am new to this I am not quite sure whats wrong with the image code and I do not know where to go to from here. Any help to sort out the image code lines so the images populate in the UITableview would be appreciated.

My code is as follows:

#import "ViewController.h"
#import "MenuViewCell.h"

@interface ViewController ()

@property (copy, nonatomic) NSArray* tableData;

@end

@implementation ViewController


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableData = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"TestList" ofType: @"plist"]];
    NSLog(@"%@",_tableData);

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:(BOOL)animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_tableData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    //return [[[_tableData objectAtIndex: section] objectForKey: @"Rows"] count];
    NSDictionary *dataInSection = [[_tableData objectAtIndex: section] objectForKey: @"Rows"];
    return [dataInSection count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    return [[_tableData objectAtIndex: section] objectForKey: @"Title"];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"MenuCell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *sectionData = _tableData[indexPath.section];
    NSArray *rowsData = sectionData[@"Rows"];
    NSDictionary *rowData = rowsData[indexPath.row];


    UILabel *homeLabel = (UILabel *)[cell viewWithTag:100];
    homeLabel.text = rowData[@"home"];

    UILabel *homeScoreLabel = (UILabel *)[cell viewWithTag:101];
    homeScoreLabel.text = rowData[@"homeScore"];

    UILabel *awayLabel = (UILabel *)[cell viewWithTag:102];
    awayLabel.text = rowData[@"away"];

    UILabel *awayScoreLabel = (UILabel *)[cell viewWithTag:103];
    awayScoreLabel.text = rowData[@"awayScore"];

    UIImageView *homeImage = (UIImageView *)[cell viewWithTag:104];
    homeImage.image = rowData[@"homeImage"];


    UIImageView *awayImage = (UIImageView *)[cell viewWithTag:105];
    awayImage.image = rowData[@"awayImage"];

    return cell;
}

- (void)dealloc {
    [_tableData release];
    [super dealloc];
}
@end

At a minimum I would expect you need to do something like:

homeImage.image = [UIImage imageWithData:[rowData[@"homeImage"]];

Have you checked via debugging that _tableData contains the contents of the plist in the way you expect?

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