[英]Load UIImageView with TableView to another ViewController
我希望你能幫幫我!
現在我有一個來自此源的表格視圖按組的2014年世界杯國家的iOS表格視圖
在這里,您可以從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對象沒有任何關系! 問題出在哪里?
謝謝!
您需要使用[[tableArr objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]]來從數組中獲取國家/地區的名稱,因為該數組是一個數組數組。
NSString *photoFilename = [NSString stringWithFormat:@"%@_photo.png", tableArr[indexPath.section][indexPath.row]];
您在prepareForSegue:
中傳遞了一個字符串prepareForSegue:
這不是照片名稱。 因此,不會填充圖像視圖。
當您在單元格中獲取圖像時,它可能仍然有效:
NSString *tempCountry = tableArr[indexPath.section][indexPath.row];
imageView.image=[UIImage imageNamed:tempCountry];
圖像名稱大概是@"BRAZIL"
,它對@"BRAZIL.png"
也有效。
但是,在准備序列時,您僅傳遞了組數組:
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
子類(如Group
和Team
等),則將獲得更具可讀性的代碼。 如果您打算保留比分和比賽時間等,則實際上應該從一開始就考慮使用Core Data。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.