1

我在尝试设置将由另一个类设置的属性时遇到问题,这里有两个属性将用于填充表:

@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 类中。

如果我尝试在viewDidLoadTableViewController 类中打印两个数组的内容,那么它将为空,并且这些数组也是在表格出现在屏幕上之前设置的。

4

2 回答 2

2

你做错了......你应该设置MyFaceboo delegateUITableViewController's tableView的数据源并实施UITableViewDataSourceProtocol

于 2012-07-16T17:27:49.953 回答
1

Assuming that you would use value of friendNames & friendIds from with in UITableViewController (that would implement UITableViewDataSource .. I do not see it in your snippet so mentioning it)

Can you re-write your properties as (assuming you are using ARC):

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

and use this syntax:

tableController.friendNames = friendNames;
tableController.friendIDs = frinedIDs

and make sure that you do show the tableController that you are creating here and not some other (it is not clear from your snippet how you are showing/pushing this tableController so mentioning it)

于 2012-07-16T17:40:08.023 回答