2011-11-06 30 views
0

我想在UITableView中显示朋友列表。用动态数据初始化UITableView

我加载的朋友:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self apiGraphFriends]; 
} 

然后我设置我的结果:

- (void)request:(FBRequest *)request didLoad:(id)result 
{ 
friends = [[NSMutableArray alloc] initWithCapacity:1]; 
NSArray *resultData = [result objectForKey:@"data"]; 
if ([resultData count] > 0) { 
    for (NSUInteger i=0; i<[resultData count]; i++) { 
     [friends addObject:[resultData objectAtIndex:i]]; 
    } 
} else { 
    //[self showMessage:@"You have no friends."]; 
} 

} 

和我采取必要的UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [friends count]; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    //NSManagedObjectModel *friend [fetch 

    FriendCell *cell = [tableView dequeueReusableCellWithIdentifier: @"friendCell"];   

    cell.cellName.text = [friends objectAtIndex:indexPath.row]; 

return cell; 
} 

的问题是,在我的数据到达之前调用方法'cellForRowAtIndexPath',你如何prev如何自动初始化表视图,并且只有在有数据时才初始化它?

回答

0

只需调用reloadData一旦你得到了你的数据,tableview将重新加载它的数据,从而调用你所有的数据源方法来获取它的数据。

2

你如何防止表视图

你不自动初始化。表格视图会在创建和显示时自动尝试加载其数据。但这应该不成问题。

并且只有当它有数据时才初始化它。

一旦数据准备就发送reloadData消息到表视图。

+0

谢谢,就像一个魅力:) – Brett

1

您是否记得将零件的数量从0更改为至少1?

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

全面测试这一点,你可以使用下面的代码

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //Add your friend as you initialise the array 
    NSMutableArray *array = [NSMutableArray arrayWithObjects:f1,nil]; 
} 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [self.friends count]; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    //change this to whatever you need 
    NSString *friend = [self.friends objectAtIndex:indexPath.row]; 
    cell.textLabel.text = friend; 

    return cell; 
} 
+0

没有必要,默认值为1. –

+0

当您创建一个新的表视图控制器在tablview控制器中的默认返回0添加一个新的UITableViewController到您的项目,并看看numberOfSectionsInTableView方法你会看到返回0;用可能不完整的方法实施警告。 –

-1

你是如此接近!

- (void)viewDidLoad 
{ 
    [self apiGraphFriends]; 
    [super viewDidLoad]; 
} 

应该这样做。如果没有,你可以在tableView上始终调用reloadData

+0

这没有奏效......重新加载虽然 – Brett

4

tableview始终在启动时加载。相反,数据加载完成后,调用

[self.tableView reloadData]; 

这告诉的tableView刷新,并再次呼吁的cellForRowAtIndexPath和所有的爵士乐。

1

如果您没有任何显示内容,您的numberOfRowsInSection应返回0。数据加载并准备显示后,您应该在tableView上调用reloadData,并且只有这样才能显示数据。 numberOfRowsInSection现在应该给你加载的行数。

总之,你不会阻止tableView初始化。你最初告诉它不显示数据,一旦你的数据被加载,你告诉它显示尽可能多的行。

+0

这并不严格,如果他在故事板中使用原型单元格dequeueReusableCellWithIdentifier会在需要时分配新单元格,因此您不必分配它。事实上,如果您选择检查您是否可以像往常一样添加一个if来检查单元格== nil,并在该block中分配一个新单元格,并且如果添加一个中断点,您将看到您的中断点永远不会到达。 –

+0

这是正确的,我正在使用故事板,并且我复制了故事板上WWDC 2011演示文稿的代码... – Brett

+1

请原谅我的无知......但我说的其余部分仍然很好。 –

1

直到获得数据后才设置tableview委托和数据源。

-(void)viewDidLoad{ 
    [self apiGraphFriends]; 
    self.tableview.delegate = self; 
    self.tableview.datasource = self; 
} 

我假设你的方法“apiGraphFriends”不使用任何背景线程,而不是异步的。如果是,则只需创建一个新方法并将数据源/委托设置放在那里,然后从apiGraphFriends方法中的块中调用它。