开发者

Loading an image into UITableViewCell from an NSArray

I have several hunderd .jpgs in /Resources within an iOS project.

Here is the viewDidLoad method:

- (void)viewDidLoad {


     NSArray *allPosters = [[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:@"."];

    [super viewDidLoad];

}

The above successfully loads all of the .jpgs into an NSArray. I simply need to display开发者_如何转开发 all of the .jpgs within this array in UITableViewCells in a UITableView

Here is the -(UITableViewCell *)tableView:cellForRowAtIndexPath: method:

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

    static NSString *CellIdentifier = @"Cell";

    NSDictionary *posterDict = [allPosters objectAtIndex:indexPath.row];
    NSString *pathToPoster= [posterDict objectForKey:@"image"];

    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell ==nil ) {

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


            }

    UIImage *theImage = [UIImage imageNamed:pathToPoster];
    cell.ImageView.image = [allPosters objectAtIndex:indexPath.row];
    return cell;
}   

I know the issue is with cell.ImageView.image, but am not sure what the issue is? How can I grab each .jpg from the array and display in each row?


NSArray *allPosters = [[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:@"."];

This will give you an array of paths (as suggested by the method name). Those paths are NSStrings. But you are assigning this array to a local variable, and this variable will be gone after you left viewDidLoad.

so you have to change it into something like this:

allPosters = [[[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:@"."] retain];

Another one:

NSDictionary *posterDict = [allPosters objectAtIndex:indexPath.row];
NSString *pathToPoster= [posterDict objectForKey:@"image"];

And this would definitely crash if you would have assigned the array correctly.

Change it into

NSString *pathToPoster = [allPosters objectAtIndex:indexPath.row];

Next one:

UIImage *theImage = [UIImage imageNamed:pathToPoster];
cell.ImageView.image = [allPosters objectAtIndex:indexPath.row];

UIImages imageNamed: doesn't work with paths, it needs filenames. And of course you want to assign the real image to the imageview and not the path to the poster. So change it:

UIImage *theImage = [UIImage imageNamed:[pathToPoster lastPathComponent]];
cell.imageView.image = theImage;


Try using [UIImage imageWithContentsOfFile:pathToPoster] rather than imageNamed. And set the value to the UIImage object.


It's probably just a typo, but it should be:

//lowercase "i" in imageView
cell.imageView.image = [allPosters objectAtIndex:indexPath.row]; 

Also- you are creating UIImage *theImage, but then you don't use it. What's going on there?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜