2

私はこのiPhoneプロジェクトを行っており、通常のView Controller内に(動的な)テーブルビューが必要です。ページに他のものを配置する必要があるため、テーブルビューコントローラーを選択しませんでした。テキストフィールド、ボタン、および約4〜5個のセルの小さなテーブルビューがあるページを想像してみてください。

アプリを実行するときは、ボタンをタッチしてこのビューに移動する必要があります。ボタンをクリックすると、アプリがクラッシュし、次のように通知されます。

2012-07-22 14:40:57.304いくつですか?[3055:f803] * -[UITableView _createPreparedCellForGlobalRow:withIndexPath:]、/ SourceCache / UIKit_Sim / UIKit-1914.84 / UITableView.m:6061でアサーションが失敗しました

これは私の.Hファイルです:

#import <UIKit/UIKit.h>

@interface ProjectViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@end

これは私の.Mファイルです:

#import "ProjectViewController.h"

@interface ProjectViewController ()

@end

@implementation ProjectViewController


//MyViewController.m
#pragma mark - Table View Data Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"numberOfSectionsInTableView");
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection");
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cellForRowAtIndexPath");

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

    NSLog(@"cellForRowAtIndexPath");

    cell.textLabel.text = @"Test";

    return cell;
}


@end

テーブルビューからコントローラーにcontrol-draggedして、デリゲートとデータソースを設定します。

私は何を間違えましたか?

ご協力いただきありがとうございます。

4

2 に答える 2

2

ファイルにコンセントを作成して、.hファイルに配線してみtableViewてくださいstoryboard

ProjectViewController.h

@property (nonatomic, strong) IBOutlet UITableView *myTableView;

ProjectViewController.m

@synthesize myTableView;

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cellForRowAtIndexPath");

    UITableViewCell *cell = [self.myTableView dequeueReusableCellWithIdentifier:@"cell"];

    NSLog(@"cellForRowAtIndexPath");

cell.textLabel.text = @"Test";

return cell;

}

于 2012-07-22T19:31:36.627 に答える
0

-cellForRowAtIndexPath では、これを行うと問題ありません。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cellForRowAtIndexPath");

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

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

NSLog(@"cellForRowAtIndexPath");

cell.textLabel.text = @"Test";

return cell;

}

問題は、セルにメモリが割り当てられず、初期化されていないことです。そのため、エラーが発生します。

于 2012-07-23T14:44:54.707 に答える