1

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

テーブルに関して。フィッシュ リストに項目 1 を 1 つだけ追加したので、セルを 1 つだけ表示する必要があります。

4

1 に答える 1

0

メソッドinitをオーバーライドする必要はありません。initWithStyle:コードを から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;最後に入れる。ここで編集されMarineFishViewController.mます。

于 2013-03-12T01:37:42.390 に答える