1

I did an empty project on UITable Cells and it worked. Using the same code I added in to an existing project where there are multiple xib linked. When navigating from another xib to this page, the table cells did appear but the content and the required number of cells did not run.

Below is the code of my current project:

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 =@"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

At the current sub class: marine fish

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

Regarding to the table. I only added one item to the fish list which is item1 and hence should only have one cell in display.

4

1 回答 1

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;最后。这里是编辑的MarineFishViewController.m

于 2013-03-12T01:37:42.390 回答