2013-04-03 110 views
2

我仍然是一個obj-C,但我不清楚,我正在創建一個應用程序,當我添加項目到tableview時需要刷新一個tableview。我GOOGLE了一下,發現一些答案,說我需要使用[self.tableView reloadData],但它不適合我。加載視圖後無法更新UITableView

這裏是我的代碼爲我的tableView

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

- (NSString *) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger)section { 
    NSString *myTitle = [[NSString alloc] initWithFormat:@"Runs"]; 
    return myTitle; 
     } 

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

    return [entries count]; 
    } 

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier 
forIndexPath:indexPath]; 

    // Set up the cell... 
    NSString *cellValue = [entries objectAtIndex:indexPath.row]; 
    cell.textLabel.text = cellValue; 

    return cell; 
} 

這裏是我填充表

- (void)populateTable{ 
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
    NSString *username = appDelegate.username; 
    entries = [[NSMutableArray alloc]init ]; 
    [self openDB]; 
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Savings WHERE username = '%@'",username]; 
    sqlite3_stmt *statement; 

    if(sqlite3_prepare_v2(runnerAppDB, [sql UTF8String], -1, &statement, nil)==SQLITE_OK) 
    { 
     while(sqlite3_step(statement)==SQLITE_ROW) 
     { 

      char *field1 = (char *) sqlite3_column_text(statement, 0); 
      NSString *field1Str = [[NSString alloc] initWithUTF8String:field1]; 

      // username - distance - speed - date - comment 
      NSString *str = [[NSString alloc] initWithFormat:@"%@ %@ %@ - %@", field1Str]; 
      [entries addObject:str]; 

     } 
    } } 

我已經嘗試做的是調用populateTable功能每次我添加一些代碼到數據庫,但仍然不刷新,請有人可以幫我嗎?一段時間以來一直困擾着這個問題。

感謝

+0

您的代碼不會創建一個單元格開始,使用調試器檢查它返回的內容,我認爲它將爲零。 – Tim

+0

邁克爾,在開始爲你編寫代碼之前,你應該先掌握iOS開發。 – samfisher

回答

1

試試吧....

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:YES]; 
    [self.myTableView reloadData]; 
} 
+0

@Michael:發生了什麼告訴我.... –

+0

它在「self.myTableView」上給出錯誤。在我輸入「self」後,我甚至沒有任何有關我的tableView的自動完成功能。你知道爲什麼嗎 ?我不知道它是什麼錯,因爲我可以在tableView中的值,我只是不能更新新的。謝謝 – Michael

+0

IBOutlet UITableView * myTableView;聲明爲.h文件 現在將其設置爲.xib文件 現在[myTableView reloadData];工作 –

0

您需要重新加載你的表之後的所有數據是你的陣列從中你感覺你的表所示。 並在tableview的cellforrowatindexpath方法中需要重用該單元格。請更新您的cellforrow方法。

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

     static NSString *cellIdentifier = @"ArticleCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; 
    } 

    NSString *cellValue = [entries objectAtIndex:indexPath.row]; 
    cell.textLabel.text = cellValue; 

    return cell; 

} 
+0

我試過了,但不起作用 – Michael

+0

你確定你的條目數組從數據庫獲取所有數據? 或者得到另一種獲取數據的方式,並確保使用NSLog更新數據。獲取所有數據後,添加[urTableView reloadData]; 這是標準的實施方式。 – iCoder

+0

我可以在我的tableView中看到值,所以我想我可以訪問數據庫中的數據..我的問題是,我添加了一些新的數據庫後,我無法刷新我的viewTable – Michael

0

一旦表的數據源得到改變,那麼只有你需要重新加載表。只要檢查一次數據源是否受到影響就重新加載表視圖。喜歡這個。

- (void)populateTable{ 
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
    NSString *username = appDelegate.username; 
    entries = [[NSMutableArray alloc]init ]; 
    [self openDB]; 
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Savings WHERE username = '%@'",username]; 
    sqlite3_stmt *statement; 

    if(sqlite3_prepare_v2(runnerAppDB, [sql UTF8String], -1, &statement, nil)==SQLITE_OK) 
    { 
     while(sqlite3_step(statement)==SQLITE_ROW) 
     { 

      char *field1 = (char *) sqlite3_column_text(statement, 0); 
      NSString *field1Str = [[NSString alloc] initWithUTF8String:field1]; 

      // username - distance - speed - date - comment 
      NSString *str = [[NSString alloc] initWithFormat:@"%@ %@ %@ - %@", field1Str]; 
      [entries addObject:str]; 

     } 
    } 
    [self.tableView reloadData] 
} 
+0

我有這個問題是,它不刷新。我的應用程序有3個選項卡,第二個是與tableView的。它只保存我第一次點擊標籤的狀態。所以如果我添加一些東西,並在第二個選項卡上,值將被添加,但如果我再次添加它將保留在'舊'版本。 – Michael