2016-01-14 28 views
0

我已經看到幾個類似的帖子關於原型單元格不更新,並試圖通過一堆答案後,我必須張貼我自己的問題。像許多人一樣,我創建了UITableViewCell的一個子類,並將多個屬性鏈接到Xcode中的原型單元格。自定義UITableViewCells不更新(原型單元格)

自定單元的.H

@property (strong, nonatomic) IBOutlet UILabel *playerNameLabel; 
@property (strong, nonatomic) IBOutlet UILabel *positionLabel; 
@property (strong, nonatomic) IBOutlet UILabel *playersPositionLabel; 
@property (strong, nonatomic) IBOutlet UILabel *teamLabel; 
@property (strong, nonatomic) IBOutlet UILabel *playersTeamLabel; 
@property (strong, nonatomic) IBOutlet UIImageView *logoView; 
@property (strong, nonatomic) IBOutlet UIView *layerView; 

而且在我的ViewController文件,我使用下面的代碼。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
PlayerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"playerCell" forIndexPath:indexPath]; 

if ([self.playerSearchBar.text isEqualToString: @""]) { 

    Player *player = self.playerArray[indexPath.row]; 

    cell.playerNameLabel.text = player.fullName; 
    cell.playersPositionLabel.text = player.position; 
    cell.playersTeamLabel.text = player.team; 

} else { 

    Player *player = self.filteredPlayerArray[indexPath.row]; 

    cell.playerNameLabel.text = player.fullName; 
    cell.playersPositionLabel.text = player.position; 
    cell.playersTeamLabel.text = player.team; 

} 
return cell; 
} 

有趣的是,數據在那裏,我可以在調試器中看到它,但沒有標籤更新。以前我用標籤做過這件事,但由於我使得單元格的設計更加複雜,我已經轉向了子類化。有什麼我可能會在這裏失蹤?

Screenshot

+0

可以肯定的是:你在界面構建器中鏈接過網點嗎? –

+0

當然!三重檢查。 – pennyloaf

+0

你註冊了你的自定義單元嗎? –

回答

0

所以這個解決方案有點莫名其妙。我刪除了所有的IBOutlets並重新創建並重新連接它們。現在,一切都按照原來的方式工作。對於上下文,在我刪除它們之前,它們已正確連接到故事板。

-1

始終cellForRowAtIndexPath方法返回相同的小區。所以請更新您的方法第一行如下。

PlayerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"playerCell"]; 

這可以解決問題。假設你在故事板中有正確的identifier名稱,即playerCell

+1

dequeueReusableCellWithIdentifier:只比dequeueReusableCellWithIdentifier以上:forIndexPath: Apple建議使用較新的版本 –

+0

FYI着,當然蘋果可以推薦,但按照需要這傢伙有,也沒有使用dequeueReusableCellWithIdentifier的任何優勢:forIndexPath:。 –

0

我認爲最有可能你缺少這個..

[self.tableView registerNib:[UINib nibWithNibName:@"YourCustomCellNibName" bundle:nil] 
forCellReuseIdentifier:@"playerCell"]; 

的tableView:的cellForRowAtIndexPath:,當我們調用dequeueReusableCellWithIdentifier:forIndexPath :,如果表中有沒有免費的可重複使用的電池已經存在,筆尖會自動加載,單元格將被實例化並返回給我們。

0

如果您在Storyboard中配置它,則不需要註冊您的單元格。如截圖所示,單元格正確顯示,因此註冊過程成功。如果您使用的是筆尖,如果您忘記註冊,它會崩潰,所以這不應解決上述問題。

我想到的第一件事情是,playerArrayfilteredPlayArray未設置,但它會顯示,而不是佔位符文本。所以我不認爲這會解決你的問題。無論如何,嘗試檢查。你是否還檢查過cell.playerNameLabel實際上是否設置爲非零值? 如果沒有,如果所有連接都正確,這也可能是Xcode問題。嘗試重新啓動Xcode並重新連接所有IBOutlet。

相關問題