2015-04-12 57 views
0

我遇到了第二頁中的表格視圖在第一頁導航後無法顯示的問題。導航後無法顯示錶格視圖

的第一個頁面重定向代碼爲:

- (IBAction)searchProducts:(id)sender { 
    ProductsViewController *proViewController = [[ProductsViewController alloc] init]; 
    [self.navigationController pushViewController:proViewController animated:YES]; 
} 

在productsViewController:

#import "ProductsViewController.h" 
#import "OriginViewController.h" 
#import "Product.h" 

@interface ProductsViewController() 

@end 

@implementation ProductsViewController 
NSArray *products; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    Product *product1 = [Product new]; 
    product1.name = @"馬克杯"; 
    product1.detail = @"¥57.00 - 64.00"; 
    product1.imageFile = @"cup1.jpg"; 

    products = [NSArray arrayWithObjects:product1, nil]; 

    UIEdgeInsets inset = UIEdgeInsetsMake(5, 0, 0, 0); 
    self.tableView.contentInset = inset; 
} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return products.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"INIT CELL."); 

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    Product *product = [products objectAtIndex:indexPath.row]; 
    UIImageView *productImageView = (UIImageView *)[cell viewWithTag:100]; 
    productImageView.image = [UIImage imageNamed:product.imageFile]; 

    UILabel *productNameLabel = (UILabel *)[cell viewWithTag:101]; 
    productNameLabel.text = product.name; 

    UILabel *productDetailLabel = (UILabel *)[cell viewWithTag:102]; 
    productDetailLabel.text = product.detail; 

    return cell; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

@end 

兩點可以肯定:

  • 的cellForRowAtIndexPath方法已被調用。
  • ViewDidLoad已被調用。

有人對我有提示嗎?謝謝。

+0

如果我在產品控制器的第一頁和'模態'中添加了按鈕,它工作正常。 – Linqtoxml

+0

在調用viewDidLoad之後檢查self.view.frame。 – ninjaproger

+0

檢查表視圖框架。 – vojer

回答

0

如果您使用UITableViewCell而不使用自定義標籤,則不需要設置標籤。

你可以做

[cell.imageView setImage:[UIImage imageNamed:product.imageFile]]; 
[cell.textLabel setText:product.name]; 
[cell.detailLabel setText:product.detail]; 

如果使用的是自定義單元格,那麼你可以設置標籤檢索觀點或只設置屬性或只要你想,你可以採取任何方式。但是,如果您使用的是Apple的默認設置UITableViewCell,則默認情況下會爲您提供特定類型的單元格,其中包含label s和imageView s。

請看here瞭解更多信息。

0

您填充數組後,嘗試調用

[myTableView reloadData];

它調用UITableView的委託方法,並將數據設置爲數組中的單元格。

相關問題