1

別のクラスによって設定されるプロパティを設定しようとすると問題が発生します。テーブルにデータを入力するために使用される 2 つのプロパティを次に示します。

@interface TableViewController : UITableViewController
    @property (nonatomic, retain) NSArray *friendsNames;
    @property (nonatomic, retain) NSArray *friendsIDs;
@end

このクラス(から継承NSObject)では、このメソッドを使用してそれらを設定しています:

@implementation MyFacebooDelegate

-(void)getFriends:(NSArray *)names {
    NSMutableArray *friendsNames = [[NSMutableArray alloc] init];
    NSMutableArray *friendsIDs = [[NSMutableArray alloc] init];
    for (int i=0; i<[names count]; i++) {
        NSDictionary *friend = [names objectAtIndex:i];
        NSString *name = [friend objectForKey:@"name"];
        [friendsIDs addObject:[friend objectForKey:@"id"]];
        [friendsNames addObject:name];
    }

    if(tableController == nil)
        tableController = [[TableViewController alloc] init];

    [tableController setFriendsIDs:friendsIDs];
    [tableController setFriendsNames:friendsNames];
    NSLog(@"%@", [tableController friendsNames]); 

    [friendsNames release];
    [friendsIDs release];
}

名前はすべてコンソール ( NSLog(@"%@", [tableController friendsNames])) に表示されますが、TableViewController クラスには表示されません。

TableViewController クラスの 2 つの配列の内容を出力しようとするとviewDidLoad、null になり、テーブルが画面に表示される前に配列も設定されます。

4

2 に答える 2

2

あなたは間違った方法でやっています... のデータソースとして設定して実装する必要が ありMyFaceboo delegateますUITableViewControllertableViewUITableViewDataSourceProtocol

于 2012-07-16T17:27:49.953 に答える
1

friendNamesin で& friendIdsfromの値を使用すると仮定しますUITableViewController(それは .. を実装UITableViewDataSourceします。スニペットには表示されないので、言及します)

プロパティを次のように書き換えることはできますか (ARC を使用していると仮定):

@property (nonatomic, strong) NSArray *friendsNames;
@property (nonatomic, strong) NSArray *friendsIDs;

次の構文を使用します。

tableController.friendNames = friendNames;
tableController.friendIDs = frinedIDs

ここで作成していることを表示し、他のものを表示していないことを確認してtableControllerください(スニペットからは、これをどのように表示/プッシュしているのかが明確ではないtableControllerため、言及しています)

于 2012-07-16T17:40:08.023 に答える