2014-08-29 50 views
0

在我的iOS應用程序中,我想在加載viewController時刷新tableView的全部內容。 tableView的每個單元格都有一個textLabel,它是一個步驟對象的名稱。無法刷新tableView中的單元格textLabel

我已經確保,當我返回到viewController,加載正確的stepNames(我通過記錄步驟的名稱知道這一點)。但是,該步驟的名稱在tableView中未更新。

如何確保tableView單元格的標籤正確加載?

我如何嘗試觸發的TableView的刷新:

-(void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:YES]; 
    if(reloadSteps == YES){ 
     NSLog(@"reloading steps"); 
     NSData * stepsData = [NSData dataWithContentsOfURL:stepsURL]; 
     [self fetchProjectSteps:stepsData]; 
     [self.projectInfo reloadData]; 
     int numRows = [stepNames count]; 
     NSLog(@"numRows %i", numRows); 
     for(int i =0; i<numRows; i++){ 
      [self tableView:self.projectInfo cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 
     } 
    } 
} 

如何的tableView的每個單元呈現:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"in cellForRowAtIndexPath"); 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StepCell"]; 
    } 
    if (indexPath.row ==0) { 
     cell.textLabel.font = [UIFont systemFontOfSize:16]; 
     cell.textLabel.textColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]; 
     cell.textLabel.text = @"Project Description"; 
     cell.textAlignment = NSTextAlignmentCenter; 
    } else { 
     cell.userInteractionEnabled = YES; 
     cell.textLabel.font = [UIFont systemFontOfSize:16.0]; 
     cell.textLabel.text = [stepNames objectAtIndex:indexPath.row]; 
     NSLog(@"textLabel: %@", cell.textLabel.text); // THIS IS CORRECT, BUT DOES NOT APPEAR TO BE UPDATED IN THE TABLEVIEW 

     if(![[stepImages objectAtIndex:indexPath.row] isEqual: @""]) { 
      cell.accessoryView = [stepImages objectAtIndex:indexPath.row]; 
     } 
    } 
    return cell; 
} 

這裏是我的頭文件:

@interface EditProjectViewController : UIViewController <UIActionSheetDelegate, UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate>{ 
    @property (strong,nonatomic) IBOutlet UITableView *projectInfo; 
} 

,然後在我的實現文件:

-(void) viewDidLoad{ 
    self.projectInfo = [[UITableView alloc]init]; 
    self.projectInfo.delegate = self; 
} 

回答

1

刪除這一切:

int numRows = [stepNames count]; 
    NSLog(@"numRows %i", numRows); 
    for(int i =0; i<numRows; i++){ 
     [self tableView:self.projectInfo cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 
    } 

reloadData調用會調用cellForRowAtIndexPath:爲表視圖中的每一個細胞。只要確保你回stepNames.countnumberOfRowsInSection:

編輯

我看過了你的更新代碼。一對夫婦的事情(包括您的問題最有可能是什麼):

  1. projectInfo插座應weak,不strong(因爲它是一個IBOutlet
  2. 即使你有你的表視圖界面中的文件聯繫起來,你初始化它viewDidLoad,創建一個新的實例

更改如下:

-(void) viewDidLoad{ 
    self.projectInfo = [[UITableView alloc]init]; 
    self.projectInfo.delegate = self; 
} 

對此:

-(void) viewDidLoad{ 
    self.projectInfo.dataSource = self; 
    self.projectInfo.delegate = self; 
} 
+0

感謝您的幫助。我添加了上面的代碼,因爲reloadData沒有調用cellForRowAtIndexPath。只有使用上面的代碼才能調用cellForRowAtIndexPath。 – scientiffic 2014-08-29 20:06:57

+0

嗯,那麼有什麼地方錯了...那麼你的類符合'UITableViewDelegate'和'UITableViewDataSource'協議? – rebello95 2014-08-29 20:11:35

+0

另外,你的界面文件中爲你的表格視圖設置了「委託」和「數據源」值? 'self.projectInfo'被鏈接了嗎?檢查所有這些。 – rebello95 2014-08-29 20:12:24