2012-04-17 56 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

相關問題