2013-08-12 67 views
0

我有一個帶有30個對象的UITableView。 控制器正確顯示第13行,第14行使用「joker」行更改其內容滾動,然後再以第13行和「joker」行重新開始,直到結束。TableView重複第13行

這是cellForRowAtIndexPath方法的代碼:

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

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

    UIImageView * flagImageView = (UIImageView *) [self.view viewWithTag:1]; 
    UILabel * nationLabel = (UILabel *) [self.view viewWithTag:2]; 

    nationLabel.text = [_nationsArray objectAtIndex:indexPath.row]; 
    nationLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; 

    NSLog(@"%i", indexPath.row); 

    return cell; 
} 

奇怪的是,在if (cell == nil) { ... }配置細胞不起作用......

+0

什麼是「joker」行?你能不能顯示你的控制器的其他部分? – Stas

+0

是顯示正在加載的行的行。它很難解釋和很奇怪的事情;如果你使用的是xCode 5 DP,那麼我發佈代碼 –

+0

我想知道你爲什麼得到13行。你沒有對你的細胞做任何事情!! – CRDave

回答

0

不要使用此

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

變化您的代碼:

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

      UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: @"cell"]; 
      if(cell == nil) 
      { 
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];  
      } 

      UIImageView * flagImageView = (UIImageView *) [self.view viewWithTag:1]; 
      UILabel * nationLabel = (UILabel *) [self.view viewWithTag:2]; 

      nationLabel.text = [_nationsArray objectAtIndex:indexPath.row]; 
      nationLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; 

      NSLog(@"%i", indexPath.row); 

      return cell; 
    } 
+0

謝謝!什麼都是? (在[UITableViewCell ALL] initWithReuseIdentifier:@「cell」];) –

+0

我剛剛編輯檢查 –

+0

問題持續存在... –

0

這個問題似乎與國家標籤有關。當我更換

UILabel * nationLabel = (UILabel *) [self.view viewWithTag:2]; 

nationLabel.text = [_nationsArray objectAtIndex:indexPath.row]; 

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

它的正常工作(至少文本)。所以我認爲你應該嘗試製作一個UITableViewCell子類,爲該標籤創建屬性,然後查看它是否正常。

+0

它的工作原理是因爲它在單元格樣式的默認標籤中鍵入數字,它不會在故事板中使用我的標籤,所以使用UIIMageView它不會顯示... –

+0

它在默認標籤中工作,所以我想這個問題必須以viewWithTag方式獲取標籤(實際上這是我第一次看到有人通過標籤tableView viewWithTag)。如果它是標準的方式 - UITableViewCell的子類和屬性,它工作正常。 –