UITableViewCell UITableViewCell reusability doesn't work. The cells are not refreshing their content while scrolling the page. Instead of it, the new image is put upon the old one. What am I doing wrong?
- (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;}
UPDATE!!! So prepareForReuse really works. But isn't it against MVC - writing code for dynamic tableview cell in UITableViewCell subclass? Shouldn't I create a View Controller for my custom UITableViewCell?
In ChartCell class
, use prepareForReuse()
method to clear your cell
func prepareForReuse() {
// clear pie chart
// clear bar chart
// clear name etc.
}
Actually we are reusing the view. So if you added a view to the cell's View once, it will be there. You have to remove the view from superview and get ready to show the another cell's data.
You can clear the existing views by overriding prepareForReuse
method in UITableViewCell
class.
- (void)prepareForReuse {
[self clearPieChart];
[self clearBarChart];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.