简体   繁体   中英

Load UIImageView with TableView to another ViewController

i hope you can help me!

Now i have a table view from this source an iOS table view of 2014 world cup countries by groups

Here you can see the code from the sidebarviewcontroller:

- (void)viewDidLoad

[super viewDidLoad];

self.view.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f];
self.tableView.backgroundColor = [UIColor colorWithWhite:0.9f alpha:1.0f];
self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];

grpArr = [[NSArray alloc]initWithObjects:@"Group A",@"Group B",@"Group C",@"Group D",@"Group E",@"Group F",@"Group G",@"Group H", nil];

NSArray* aArr = [[NSArray alloc]initWithObjects:@"BRAZIL",@"CROATIA",@"MEXICO",@"CAMEROON", nil];
NSArray* bArr = [[NSArray alloc]initWithObjects:@"SPAIN",@"NETHERLAND",@"CHILE",@"AUSTRALIA", nil];
NSArray* cArr = [[NSArray alloc]initWithObjects:@"COLOMBIA",@"GREECE",@"CÔTE D'IVOIRE",@"JAPAN", nil];
NSArray* dArr = [[NSArray alloc]initWithObjects:@"URUGUAY",@"COSTA RICA",@"ENGLAND",@"ITALY", nil];
NSArray* eArr = [[NSArray alloc]initWithObjects:@"SWITZERLAND",@"ECUADOR",@"FRANCE",@"HONDURAS", nil];
NSArray* fArr = [[NSArray alloc]initWithObjects:@"ARGENTINA",@"BOSNIA AND HERZEGOVINA",@"IRAN",@"NIGERIA", nil];
NSArray* gArr = [[NSArray alloc]initWithObjects:@"GERMANY",@"PORTUGAL",@"GHANA",@"USA", nil];
NSArray* hArr = [[NSArray alloc]initWithObjects:@"BELGIUM",@"ALGERIA",@"RUSSIA",@"KOREA REPUBLIC", nil];

tableArr = [NSArray arrayWithObjects:aArr,bArr,cArr,dArr,eArr,fArr,gArr,hArr, nil];

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

return [grpArr count];

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


return  [grpArr objectAtIndex:section];

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

return  [[tableArr objectAtIndex:section] count];

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

return 44;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"normalCell"];

NSString *tempCountry = [[tableArr objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];
UIImageView *imageView = (UIImageView *)[[cell contentView] viewWithTag:1];
imageView.image=[UIImage imageNamed:tempCountry];
UILabel *countryLabel = (UILabel*)[[cell contentView] viewWithTag:2];
countryLabel.text = tempCountry;


return cell;

- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender

{

// Set the photo if it navigates to the PhotoView
if ([segue.identifier isEqualToString:@"showPhoto"]) {
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    PhotoViewController *photoController = (PhotoViewController*)segue.destinationViewController;
    NSString *photoFilename = [NSString stringWithFormat:@"%@_photo.png", [tableArr objectAtIndex:indexPath.row]];
    photoController.photoFilename = photoFilename;
}

if ( [segue isKindOfClass: [SWRevealViewControllerSegue class]] ) {
    SWRevealViewControllerSegue *swSegue = (SWRevealViewControllerSegue*) segue;

    swSegue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc) {

        UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
        [navController setViewControllers: @[dvc] animated: NO ];
        [self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
    };

}

}

In my PhotoViewController i load the image in the viewDidLoad Method with the following line of code

 self.photoImageView.image = [UIImage imageNamed:self.photoFilename];

The problem is i cant load the image in the photoviewcontroller, the image is not showing. I want to show a ball with the brazil flag. my image named BRAZIL_photo.png. I think i have no connection to the nsarray objects!? Where is the issue?

Thank you!

您需要使用[[tableArr objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]]来从数组中获取国家/地区的名称,因为该数组是一个数组数组。

NSString *photoFilename = [NSString stringWithFormat:@"%@_photo.png", tableArr[indexPath.section][indexPath.row]];

You are passing a string in prepareForSegue: that is not the photo name. Therefore, the image view does not get populated.

When you get the image in the cell it presumably still works:

NSString *tempCountry = tableArr[indexPath.section][indexPath.row];
imageView.image=[UIImage imageNamed:tempCountry];

The image name is presumably @"BRAZIL" which is also valid for @"BRAZIL.png" .

When preparing for the segue, however, you are passing only the group array:

NSString *photoFilename = [NSString stringWithFormat:@"%@_photo.png", 
                           tableArr[indexPath.row]];
photoController.photoFilename = photoFilename;

The image name is now something along the lines of @"<CFArray 0x45003F>.png" . So you should pass the proper string instead:

    NSString *photoFilename = [NSString stringWithFormat:@"%@_photo.png", 
                           tableArr[indexPath.section][indexPath.row]];

Now the image name should be @"BRAZIL_photo.png" . Check your file names (including capitalisation) and you should find the error.

Your problem arises from your absurd data setup. One correct way to do this is to have a plist that holds the strings. You would achieve much more readable code if you created NSObject subclasses like Group and Team etc. with the proper attributes. If you intend to keep scores and game times etc., you should actually consider using Core Data from the beginning.

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