2012-06-29 8 views
15

これは私の問題です:私は私のストーリーボードで、この小さなUITableViewを持って :enter image description here設定のUITableViewのデリゲートとデータソース

そして、これは私のコードです:

SmallTableViewController.h

#import <UIKit/UIKit.h> 
#import "SmallTable.h" 

@interface SmallViewController : UIViewController 

@property (weak, nonatomic) IBOutlet UITableView *myTable; 

@end 

SmallTableViewController.m

#import "SmallViewController.h" 

@interface SmallViewController() 

@end 

@implementation SmallViewController 
@synthesize myTable = _myTable; 

- (void)viewDidLoad 
{ 
    SmallTable *myTableDelegate = [[SmallTable alloc] init]; 
    [super viewDidLoad]; 
    [self.myTable setDelegate:myTableDelegate]; 
    [self.myTable setDataSource:myTableDelegate]; 

    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

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

@end 

ご覧のとおり、myTableDelegateというインスタンスをDelegateとして設定し、myTableのDataSourceを設定します。

これはSmallTableクラスのソースです。

SmallTable.h

#import <Foundation/Foundation.h> 

@interface SmallTable : NSObject <UITableViewDelegate , UITableViewDataSource> 

@end 

SmallTable.m

@implementation SmallTable 

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

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

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

    // Configure the cell... 
    cell.textLabel.text = @"Hello there!"; 

    return cell; 
} 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"Row pressed!!"); 
} 

@end 

私はアプリが必要とするすべてUITableViewDelegateUITableViewDataSource方法を実装しました。なぜそれはちょうどビューが表示される前にクラッシュ?

ありがとうございます!

+0

は、あなたがエラーを貼り付けでし設定する必要がありますか? – JonLOo

+0

クラッシュログも追加できますか? – rishi

+0

スレッドで議論を確認する - http://stackoverflow.com/questions/254354/uitableview-issue-when-using-separate-delegate-datasource – rishi

答えて

15

リッキーは右です。しかし、私はあなたのメソッドの終わりにオブジェクトがとにかく割り当てが解除されるので、strongあなたのプロパティの修飾子を使用する必要があると思います。

@property (strong,nonatomic) SmallTable *delegate; 

// inside viewDidload 

[super viewDidLoad]; 
self.delegate = [[SmallTable alloc] init];  
[self.myTable setDelegate:myTableDelegate]; 
[self.myTable setDataSource:myTableDelegate]; 

ただし、テーブルに分離オブジェクト(データソースとデリゲート)を使用する理由はありますか? SmallViewControllerをテーブルのソースとデリゲートの両方に設定しないでください。

さらに、正しい方法でセルを作成していません。これらの行は何もない:

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

// Configure the cell... 
cell.textLabel.text = @"Hello there!"; 

dequeueReusableCellWithIdentifierは、単に「キャッシュ」テーブルから、すでに作成されており、それを再利用することができます(これはメモリ消費を回避するため)が、あなたがいずれかを作成していないセルを取得していません。

ここではalloc-initをやっていますか?さらに、1の代わりに0を返すようにnumberOfSectionsInTableViewに言う

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if(!cell) { 
    cell = // alloc-init here 
} 
// Configure the cell... 
cell.textLabel.text = @"Hello there!"; 

:代わりにこれを行う

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

おそらくARCを使用していますか? myTableDelegateは、viewDidLoadのローカル変数でのみ参照されます。メソッドが終了すると、割り当てが解除されます。 (デリゲート/データソースパターンでは、オブジェクトはデリゲートを所有していないので、オブジェクトへのテーブルビューの参照は弱いです。)クラッシュを引き起こすとは思えませんが、問題の鍵となる可能性があります。

+0

OK、新しい@property(weak、nonatomic)SmallTable *デリゲートを作成しました。アプリケーションがクラッシュすることはありませんが、テーブルビューは空です!なぜ私は理解できません... –

1

setDelegateは代理人を保持しません。

そして

numberOfSectionsInTableView方法は、1の代わりに0を返すように有しています。

0

のUITableViewオブジェクトのデリゲートはUITableViewDelegateプロトコルを採用しなければなりません。プロトコルのオプションのメソッドは、代理人が選択の管理、セクションの見出しとフッターの設定、メソッドの削除に役立ちます。

enter image description here

1
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 0; 
} 

セクションの数は、少なくとも1つの

関連する問題