2013-03-12 49 views
1

我在UITable單元格上做了一個空項目並且它工作正常。使用相同的代碼,我將其添加到有多個xib鏈接的現有項目中。從另一個xib導航到此頁面時,表格單元格確實出現,但內容和所需的單元格數量未運行。當從另一個XIB導航時,UITable單元格不在XIB上顯示

下面是我的當前項目的代碼:

OptionViewController.m

#import "OptionViewController.h" 
#import "FishAppDelegate.h" 
#import "MainViewController.h" 
#import "PetFishViewController.h" 
#import "MarineFishViewController.h" 

@interface OptionViewController() 

@end 

@implementation OptionViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.title [email protected]"Choose a category :)"; 

    // Do any additional setup after loading the view from its nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(IBAction)PetFish:(id)sender 
{ 
    PetFishViewController *petFish = [[PetFishViewController alloc]initWithNibName:nil bundle:nil]; 
    [[self navigationController] pushViewController:petFish animated:YES]; 
} 


// this is the sub class containing table cells 

-(IBAction)MarineFish:(id)sender 
{ 
    MarineFishViewController *MarineFish = [[MarineFishViewController alloc]initWithNibName:nil bundle:nil]; 
    [[self navigationController] pushViewController:MarineFish animated:YES]; 
} 

@end 

在當前子類:海洋魚類

MarineFishController.h

#import <Foundation/Foundation.h> 

@interface MarineFishViewController : UITableViewController 
{ 
    NSMutableArray *fishList; 
} 

@end 

MarineFishController.m:

#import "MarineFishViewController.h" 
#import "MarineFish.h" 

@implementation MarineFishViewController 
{ 
    /*NSArray *tableData; 
    NSArray *thumbnail; 
    */ 

} 

-(id) init{ 

    //call the superclass 
    self = [super initWithStyle:UITableViewStyleGrouped]; 

    if(self){ 

     fishList = [[NSMutableArray alloc] init]; 

     MarineFish *Item1 = [[MarineFish alloc] initWithName:@"Shark" imageName:@"Sea.jpg"]; 

     [fishList addObject:Item1]; 
    } 

    return self; 
} 

-(id)initWithStyle:(UITableViewStyle)style{ 

    return [self init]; 
} 

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

    return [fishList count]; 
} 

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    //create an instance of uitableviewcell 
    //check for a reusable cell first, use if exist 

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

    //if there is no reusable cell of this type,create new one 

    if(!cell) 
    { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; 

    } 

    //set text on the cell 

    MarineFish *p = [fishList objectAtIndex:[indexPath row]]; 
    [[cell textLabel] setText:[[NSString alloc] initWithFormat:@"%@",[p fishName]]]; 

} 

@end 

關於表。我只在魚列表中添加了一個item1,因此應該只顯示一個單元格。

+0

你說你從另一個項目中帶入了代碼?您是否將相關的.m文件添加到新目標中? – 2013-03-12 01:33:43

+0

上傳項目並共享鏈接 – 2013-03-12 02:28:20

+0

http://www.mediafire.com/?teq0bc6c37ip2nn 這是該項目的鏈接,附有兩張圖片供參考。請幫忙,謝謝! – 2013fyp 2013-03-12 02:55:06

回答

0

您不需要覆蓋initinitWithStyle:方法。將你的代碼從initviewDidLoad

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    fishList = [[NSMutableArray alloc] init]; 
    MarineFish *Item1 = [[MarineFish alloc] initWithName:@"Shark" imageName:@"Sea.jpg"]; 
    [fishList addObject:Item1]; 
} 

編輯 錯誤是如此健忘......

-(UITableViewCell*)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 

必須返回一個UITableViewCell對象。所以把return cell;放在最後。 Here編輯MarineFishViewController.m

+0

嗨, @Mike,yup我把所有的代碼全部複製到這個項目中 – 2013fyp 2013-03-12 01:46:25

+0

感謝您的幫助,但我試圖移動init來查看確實加載,但同樣的問題仍然存在。有沒有其他的方式來顯示錶格單元格的內容? – 2013fyp 2013-03-12 01:48:33

+0

謝謝謝爾蓋!真正解決了我的誤差與optionviewcontrollerm改變初始化沿着: - (IBAction爲)MarineFish:(ID)發送方 { MarineFishViewController * MarineFish = [[MarineFishViewController的alloc] initWithStyle:UITableViewStyleGrouped]; [[self navigationController] pushViewController:MarineFish animated:YES]; } – 2013fyp 2013-03-13 02:34:34