簡體   English   中英

自定義UITableViewCells不更新(原型單元)

[英]Custom UITableViewCells not updating (prototype cell)

我已經看到了幾篇關於原型單元格沒有更新的類似文章,並且在嘗試了一系列答案之后,我不得不發表自己的問題。 像許多人一樣,我創建了UITableViewCell的子類,並將幾個屬性鏈接到Xcode中的原型單元。

自定義單元的.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;
}

有趣的是,數據在那里,我可以在調試器中看到它,但是標簽沒有更新。 以前,我是使用標簽來完成此操作的,但是由於我使單元的設計更加復雜,因此我轉而使用了子類化。 我這里可能缺少什么嗎?

屏幕截圖

因此,解決方案有點莫名其妙。 我刪除了所有的IBOutlet,然后重新創建它們並重新連接它們。 現在,一切都按預期進行。 就上下文而言,在我刪除它們之前,它們已正確連接到情節提要。

我認為您很可能會錯過這個。

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

tableView:cellForRowAtIndexPath:中 ,當我們調用dequeueReusableCellWithIdentifier:forIndexPath:時,如果表中沒有可用的可重用單元格,則將自動加載該筆尖,並將該單元格實例化並返回給我們。

如果您在情節提要板中配置了單元,則無需注冊。 如屏幕截圖所示,單元格正確顯示,因此注冊過程成功。 如果您使用的是筆尖,則如果您忘記注冊它就會崩潰,因此這不能解決上面建議的問題。

我想到的第一件事是可能沒有設置playerArrayfilteredPlayArray ,但隨后它只顯示占位符文本,什么也不顯示。 因此,我認為這不會解決您的問題。 無論如何,請嘗試檢查一下。 您是否還檢查過cell.playerNameLabel實際上是否設置為非nil值? 如果沒有,如果所有連接正確建立,這也可能是Xcode問題。 嘗試重新啟動Xcode並再次重新連接所有IBOutlet。

始終cellForRowAtIndexPath方法返回相同的單元格。 因此,請如下更新您的方法的第一行。

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

這樣可以解決問題。 假設您在情節提要中具有正確的identifier名稱,即playerCell

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM