2012-04-17 65 views
3

我试图更新我的UITableview,但是当我打电话给[tableView reloadData];时,表格并没有更新,之后我将tableview在单元格名称上方滚动,同时被更改。但它不是增加一个新的行或类似的东西。reloadData不起作用

-(void)update { 
     [tableView reloadData]; 

    NSLog(@" %i", [tableData count]); 
} 

当我添加一行它返回2,但表不更新。

- (void) refresh:(id)sender { 


    NSString *jsonString = [NSString 
          stringWithContentsOfURL:[NSURL URLWithString:xmlDataUrl] 
          encoding:NSUTF8StringEncoding 
          error:nil]; 


    SBJSON *parser = [[SBJSON alloc] init]; 
    NSDictionary *results = [parser objectWithString:jsonString error:nil]; 

    parser = nil; 

    [self setTableData:[results objectForKey:@"items"]]; 
     [self performSelector:@selector(update) withObject:nil afterDelay:.2]; 


} 

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

    static NSString *CellIdentifier = @"Cell"; 

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


    NSDictionary *item = [tableData objectAtIndex:[indexPath row]]; 
    [[cell textLabel] setText:[item objectForKey:@"title"]]; 
    [[cell detailTextLabel] setText:[item objectForKey:@"descript"]]; 
    [[cell detailTextLabel] setText:[item objectForKey:@"detail"]]; 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 


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

     return [tableData count]; 
} 
+0

'[self setTableData:]'做了什么,'cellForRowAtIndexPath:'方法是怎么样的? – Eimantas 2012-04-17 19:06:16

+0

[self setTableData:]正在设置cellforrowathindexpath中的数据(detailtext,image,title) – Jones 2012-04-17 19:14:15

+0

@Eimantas我编辑了消息!与cellForRowAtIndexPath: – Jones 2012-04-17 19:15:55

回答

5

每次我看到这个问题,这是UITableView插座没有连接的结果。因此,reloadData呼叫发送到nil。这在这种情况下几乎肯定是错误的。这可以通过简单的日志语句轻松测试。

-(void)update { 
    NSLog(@"tableView is '%@'",tableView); 
    [tableView reloadData]; 
    NSLog(@" %i", [tableData count]); 
} 

更可能在控制台中看到tableView is (null)

旁注: 的实现代码如下填充在所有的事实是,dataSourcedelegate出口连接的标志。

+0

修复它大声笑它我忘了连接iboutlet @property(非原子,保留)IBOutlet UITableView * tableView; – Jones 2012-04-18 14:39:49

+0

right :-) tableview不一定是零,但你可以测试它是否与self.tableView = tableView连接在一起。的cellForRowAtIndexPath – brainray 2015-06-30 06:42:54