2011-03-15 129 views
1

我正在嘗試創建一個讀取json提要的iphone應用程序,並且在更新TableView時遇到了一些麻煩。無法重新加載UITableView

這是我TableViewController頭文件:

@interface LiveNewsController : UITableViewController <UITableViewDelegate, UITableViewDataSource>{ 

NSMutableData *responseData; 
NSMutableArray *articleArray; 

UITableView *tblView; 

} 

-(void) refreshPressed: (id)sender; 

@property (nonatomic, retain) IBOutlet UITableView *tblView; 

@end 

這些都是一些部分我TableViewController實現文件:

@implementation LiveNewsController 

@synthesize tblView; 

-(id) init { 
    if(self = [super init]) 
    { 
     self.title = @"Live News"; 
    } 
    return self; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"I am here %d",[articleArray count]); 
    return [articleArray count]; 
} 


// 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 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSLog(@"cellForRowAtIndexPath called. Data size %d", [articleArray count]); 


    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [articleArray objectAtIndex:row]; 

    return cell; 
} 

-(void) refreshPressed: (id)sender 
{ 
    NSLog(@"Reload Pressed. Data size: %d", [articleArray count]); 
    [tblView reloadData]; 
} 

這是與我的.xib文件的截圖連接: IB Screenshot

所以其主要思想是:

  1. 取articleArray
  2. 刷新時按下更新視圖(數據被正確地獲取,但片段在這裏不顯示它)

numberOfRowsInSection和CellForRowAtIndexPath只在視圖加載時調用一次,但按下reload按鈕時不會發生任何事情。

我查了tblView,它是。我做了一些研究,並且讀到這通常是由.xib文件中的一些錯誤連接引起的,但我有三重檢查,並且我似乎無法找出問題所在。

任何幫助表示讚賞,

謝謝!

回答

2

你並不需要聲明UITableViewtblView,在UITableViewController已經有一個(因此所有子類也是如此)。 只需嘗試[self.tableView reloadData];

+0

我試圖刪除tblView,沒有任何改變。該應用程序仍然工作,但它最初的問題仍然存在。數據未更新。 – Pepe 2011-03-15 23:17:37

+0

它工作!雖然你的網絡連接有一個小延遲,但是你確實是對的。謝謝 ! – Pepe 2011-03-15 23:19:21