简体   繁体   English

使用TableView将UIImageView加载到另一个ViewController

[英]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 现在我有一个来自此源的表格视图按组的2014年世界杯国家的iOS表格视图

Here you can see the code from the sidebarviewcontroller: 在这里,您可以从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 在我的PhotoViewController中,我使用以下代码行将图像加载到viewDidLoad方法中

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

The problem is i cant load the image in the photoviewcontroller, the image is not showing. 问题是我无法在photoviewcontroller中加载图像,图像未显示。 I want to show a ball with the brazil flag. 我想展示一个带有巴西国旗的球。 my image named BRAZIL_photo.png. 我的图片名为BRAZIL_photo.png。 I think i have no connection to the nsarray objects!? 我认为我与nsarray对象没有任何关系! 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. 您在prepareForSegue:中传递了一个字符串prepareForSegue:这不是照片名称。 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" . 图像名称大概是@"BRAZIL" ,它对@"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" . 图像名称现在类似于@"<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" . 现在,图像名称应为@"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. 一种正确的方法是拥有一个包含字符串的plist。 You would achieve much more readable code if you created NSObject subclasses like Group and Team etc. with the proper attributes. 如果创建具有适当属性的NSObject子类(如GroupTeam等),则将获得更具可读性的代码。 If you intend to keep scores and game times etc., you should actually consider using Core Data from the beginning. 如果您打算保留比分和比赛时间等,则实际上应该从一开始就考虑使用Core Data。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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