0

マスターviewControllerにセルが表示されないという問題があります。これは状況です:

アプリはストーリーボードを使用しています。

アプリが起動すると、navigationControllerに移動します

ボタンが押されてテーブルViewControllerに接続され、「プッシュ」するように設定されています。

オブジェクトを追加して、cell/detailViewなどを作成しました。

なんらかの理由で、セルが表示されません!!!

ファイルは次のとおりです。

MasterViewController.h:

#import <UIKit/UIKit.h>
#import "CraftingDetail.h"
#import "Crafting.h"

@class CraftingList;

@interface CraftingMaster : UITableViewController

@property (strong, nonatomic) CraftingDetail *detailViewController;
@property (strong, nonatomic) CraftingList *CL;

@end

MasterViewController.m:

#import "CraftingMaster.h"
#import "CraftingList.h"

@interface CraftingMaster ()

@end

@implementation CraftingMaster

@synthesize detailViewController = _detailViewController;
@synthesize CL;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        self.CL = [[CraftingList alloc] init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for     this view controller.
     self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return  self.CL.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.CL.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...

    cell.textLabel.text = [self.CL craftingAtIndex:indexPath.row].Title;

    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath   *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:      (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
       // Delete the row from the data source
       [tableView deleteRowsAtIndexPaths:@[indexPath]     withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
       // Create a new instance of the appropriate class, insert it into the array, and  add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath   *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
     // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc]   initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end

DetailViewController.h:

#import <UIKit/UIKit.h>

@interface CraftingDetail : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *Image;
@property (strong, nonatomic) IBOutlet UITextView *Description;

@end
4

1 に答える 1

1

これは古い質問ですが、テーブルビューを使用して開発を開始する場合、予期せず空のテーブルビューを持つことは一般的な問題であるため、この回答が誰かに役立つことを願っています。

テーブルビューにセルがなく、そうでない場合に期待した場合に確認することがいくつかあります。

  • データソースオブジェクト(self.CLこの場合)は有効ですか?(つまり、それら!= nilは正しいオブジェクトを指していますか?)
  • numberOfSectionsInTableView:ゼロより大きい整数値を返しますか?
  • tableView:numberOfRowsInSection:ゼロより大きい整数値を返しますか?

上記のMasterViewController.mには、注意が必要ないくつかの問題があります。

  • InitWithStyle:ビューコントローラがストーリーボードでインスタンス化されている場合は実行されません。代わりに、initWithCoder:使用する必要があります。これがインスタンス化されなかったため、これがJomanJiの苦痛の原因だったと思いself.CLます。(余談ですが、データソースオブジェクト/プロパティ:値をプロパティではなくivarに直接CL割り当てることでインスタンス化する必要があります。理由については、「プロパティの初期化、ドット表記」を参照してください)。_CL
  • numberOfSectionsInTableView:と同じ値tableView:numberOfRowsInSection:(つまり、 " ")を返すためreturn self.CL.count;、テーブルビューには、各セクションにセルがあるのと同じ数のセクションが表示され、各セクションのセルには他のセクションと同じデータが含まれます。 。この効果が開発者の意図したものではなかったと思います。(もちろん、のcountアクセサメソッドがCraftingList本当に奇妙なことをしない限り、これはそうです)。

のコードを見ずCraftingListに、問題が何であるかを正確に判断することは不可能です。しかし、質問の年齢を考えると、JomanJiはそれ以来自分でそれを理解しているのではないかと思います。

于 2013-03-13T10:33:43.307 に答える