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

时间:2014-06-02 15:45:44

标签: ios uitableview uiimageview

我希望你能帮帮我!

现在我有来自此来源an iOS table view of 2014 world cup countries by groups

的表格视图

在这里,您可以看到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];
    };

}

}

在我的PhotoViewController中,我使用以下代码行在viewDidLoad方法中加载图像

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

问题是我无法在photoviewcontroller中加载图像,图像没有显示。我想用巴西国旗展示一个球。我的图片名为BRAZIL_photo.png。我想我与nsarray对象没有联系!? 这个问题在哪里?

谢谢!

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您在prepareForSegue:中传递的字符串不是照片名称。因此,图像视图不会填充。

当您在单元格中获得图像时,它可能仍然有效:

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

图片名称大概是@"BRAZIL",对@"BRAZIL.png"也有效。

但是,在准备segue时,您只传递组数组:

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

图像名称现在是@"<CFArray 0x45003F>.png"的行。所以你应该传递正确的字符串:

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

现在图片名称应为@"BRAZIL_photo.png"。检查您的文件名(包括大小写),您应该找到错误。

您的问题源于您荒谬的数据设置。这样做的一个正确方法是使用一个固定琴弦的plist。如果使用适当的属性创建NSObject子类(如GroupTeam等,则可以获得更易读的代码。如果您打算保持分数和游戏时间等,您应该从一开始就考虑使用核心数据。