当从另一个XIB导航时,UITable Cell不出现在XIB上

时间:2013-03-12 01:24:18

标签: ios objective-c uitableview

我在UITable Cells上做了一个空项目,但它确实有效。使用我添加到现有项目中的相同代码,其中有多个xib链接。从另一个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 =@"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

关于表格。我只在fish列表中添加了一个项目,即item1,因此只能显示一个单元格。

1 个答案:

答案 0 :(得分:0)

您无需覆盖initinitWithStyle:方法。将代码从init移至viewDidLoad

- (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