2017-05-15 14 views
1

私はテーブルビューをnsarrayでバインドしようとしていますが、Webサービスからデータを取得してnsArrayに入れました.nsarrayは正常に埋められましたが、テーブルビューのセルにバインドできません。
テーブルビューをNSArrayのコンテンツにバインドできませんか?

私は私の細胞

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellIdentifier = @"ContactCell"; 
    ContactTableViewCell *cell = (ContactTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[ContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    Contact * contact = _contacts[indexPath.row]; 
    RAC(cell.fullNameLabel , text) = RACObserve(contact,firstName); 
    RAC(cell.phoneNumberLabel , text) = RACObserve(contact,phoneNumber); 
    UIImage *contactImage = [UIImage imageNamed:@"contactPicture.png"]; 
    [cell.contactImage setImage:contactImage]; 

    return cell; 
} 

を作成し、それは私が選択したセルの内容をチェックするためのメソッドを追加しました私はNSArrayの

#import <UIKit/UIKit.h> 
#import <ReactiveCocoa/ReactiveCocoa.h> 
@interface ContactsTableView : UITableView <UITableViewDataSource,UITableViewDelegate> 
@property (strong,nonatomic) NSMutableArray * contacts; 
- (instancetype) init; 
@end 

を持っている私の.hファイルだ彼女、同じインデックスのパスにあるnsarrayは次のようになります。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    ContactTableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath]; 
    [selectedCell.contentView addSubview:selectedCell.fullNameLabel]; 
    NSLog(@"phoneLabel of cell %@", selectedCell.phoneNumberLabel.text); 
    NSLog(@"phone number of array %@", ((Contact *) _contacts[indexPath.row]).phoneNumber); 
} 

これはoutp UT:

crm-app[49692:1409964] **phoneLabel of cell (null)** 
2017-05-15 12:25:56.941 crm-app[49692:1409964] **phone number of array 0655443345** 
+0

ここにブレークポイントを追加する* contact = _contacts [indexPath.row];連絡先に値が入っているかどうか確認してください –

+0

IBOutletが接続されていないようですが、再度確認してください! –

答えて

0

UITableViewDataSourceメソッドを追加し初期化します。セルの設定

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
//this will return number of cells in your array 

return contacts.count; 
} 

は、あなたが、cellForRowAtIndexPathを表示したいデータに通常依存している:これまででセル構成を行うための最も一般的な場所です。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

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

// YOUR CODE HERE 

    return cell; 
} 

また、サーバーからデータを受信した後にテーブルをリロードする必要があります。

[yourTableView reloadData]; 

注:はまた、あなたは、テーブルビューのデリゲートとデータソースを設定していることを確認してください。

+0

ありがとうございます[myTableView reloadData]を追加するのを忘れました。私の問題だった私のデータを入手した後(y) – SAM

0

最初のクラッシュ を削除

- (void)viewDidLoad { 
     [super viewDidLoad]; 
     contacts = [[NSMutableArray alloc ]init]; 
    } 

ようNSMutableArrayあなたはUITableView代表者とデータソースのメソッドを実装する必要があり、あなたのコントローラに

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section  { 

    return [contacts count]; 
} 
関連する問題