[英]UITableViewCell reusability doesn't work
UITableViewCell UITableViewCell 可重用性不起作用。 滾動頁面時,單元格不會刷新其內容。 取而代之的是,新形象被置於舊形象之上。 我究竟做錯了什么?
- (ChartCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ChartCell *cell = (ChartCell *)[tableView dequeueReusableCellWithIdentifier:@"graphCell" forIndexPath:indexPath];
NSString* object_id = dataDict[@"cellIDs"][indexPath.row];
[[NetworkManager sharedSource] getData:[object_id integerValue] Success:^(NSDictionary *data) {
dispatch_async(dispatch_get_main_queue(), ^{
cell.cellID = [data[@"chartType"] integerValue];
//cell.textLabel.text = data[@"chartType"];
switch (cell.cellID) {
case 0:
for(int i=0; i<[data[@"chartData"] count]; i++){
cell.barNames[i] = data[@"chartData"][i][@"name"];
cell.barHeights[i] = data[@"chartData"][i][@"value"];
}
[cell createPieChart];
break;
case 1:
for(int i=0; i<[data[@"chartData"] count]; i++){
cell.barNames[i] = data[@"chartData"][i][@"name"];
cell.barHeights[i] = data[@"chartData"][i][@"value"];
}
[cell createBarChart];
break;
case 2:
break;
case 3:
break;
default:
break;
}
});
} Error:^(NSString *errorMessage) {
dispatch_async(dispatch_get_main_queue(), ^{
//показать ошибку
});
}];
return cell;}
更新!!! 所以 prepareForReuse 真的有效。 但它不是針對 MVC - 在 UITableViewCell 子類中為動態 tableview 單元格編寫代碼嗎? 我不應該為我的自定義 UITableViewCell 創建一個視圖控制器嗎?
在ChartCell class
,使用prepareForReuse()
方法清除您的單元格
func prepareForReuse() {
// clear pie chart
// clear bar chart
// clear name etc.
}
實際上,我們正在重用視圖。 因此,如果您將視圖添加到單元格的視圖一次,它將在那里。 您必須從超級視圖中刪除視圖並准備好顯示另一個單元格的數據。
您可以通過覆蓋UITableViewCell
類中的prepareForReuse
方法來清除現有視圖。
- (void)prepareForReuse {
[self clearPieChart];
[self clearBarChart];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.