2012-10-30 62 views
1

我遇到了一個很大的問題。重新加載數據(UITableView)不起作用

[self.tableView reloadData]; 

不工作,我不明白爲什麼。

[[self tableView] reloadData]; 

不工作過。

這裏是我的代碼:

.H

@interface ArticleViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate> 
{ 
} 
@end 

.M

- (void)viewDidLoad { 
[super viewDidLoad]; 
self.tableView.dataSource = self; 
self.tableView.delegate = self; 
} 

btnLeft = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btnLeft"] 
             style:UIBarButtonItemStylePlain 
             target:self 
             action:@selector(loadPlist)]; 
self.navigationItem.leftBarButtonItem = btnLeft; 

loadPlist方法,我正在寫一個文件的.plist。這部分工作正常。

一旦所有在的.plist文件中寫:

btnRight = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btnRight"] 
              style:UIBarButtonItemStylePlain 
             target:self 
             action:@selector(reloadTheTB)]; 
self.navigationItem.rightBarButtonItem = btnRight; 

- (void)reloadTheTB { 
NSLog(@"Test reloadTheTB"); 

[[self tableView] reloadData]; 
} 

如果我觸摸btnRight,我可以在日誌 「Test reloadTheTB」 中看到的。

這裏是tableView:cellForRowAtIndexPath:

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if(cell == nil) 
      cell = [self getCellContentView:CellIdentifier]; 

     contentDictio = [dict objectAtIndex:indexPath.row]; 

     UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1]; 

     lblTemp1.text = [contentDictio objectForKey:@"title"]; 

     if(indexPath.row % 2) { 
      UIView* myBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; 
      myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_grise"]]; 
      cell.backgroundView = myBackgroundView; 
     } 
     else { 
      UIView* myBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; 
      myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_blanche"]]; 
      cell.backgroundView = myBackgroundView; 
     } 
    } 

    return cell; 
} 

UPDATE:

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

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

請幫助我......

+0

,你必須創建實現代碼如下???? – Venkat

+0

那麼你正在做什麼變化,而不是''tableView reloadData''反映' – mayuur

+0

這是一個'UITableViewController'所以,我不需要創建tableView – user1269586

回答

0

其實我不知道這一點,你怎麼知道reloadData不叫?

試着把一些日誌放在cellForRowAtIndexPath?當你點擊你的按鈕時,看看是否被調用?而且在這樣做:

- (void)reloadTheTB { 
    NSLog(@"Test reloadTheTB"); 
    NSLog(@"%@",dict); 
    [[self tableView] reloadData]; 

}

是您的字典,你期待什麼內容?或者在loadPlist之後沒有改變?

可能對主線程不執行對reloadData的調用,而只能在主線程上更新UI。嘗試在主線程做這樣的事情執行你的方法:

-(void)ReloadTheTB{ 
    [self.tableView [email protected](reloadData) withObject:nil waitUntilDone:NO] 
} 
+0

plist的內容是我所期望的。 如果我在'cellForRowAtIndexPath'中做了'NSLog(@「%@」,dict)'',我就得到了我的所有字典。 如果我做'NSLog(@「%@」,dict);' - (void)reloadTheTB'中只有'()'日誌... 我真的不明白髮生了什麼 – user1269586

+0

目標是瞭解在調用reloadData之後是否看到了放在cellForRowAtIndexPath中的日誌消息。你有沒有看到日誌消息? –

+0

我沒有看到日誌消息 – user1269586