Chart reuse in UITableView cells


#1

Hi,

I’m currently trying to use a chart in some table cells, but calling [chart reloadData] after setting the data for a cell doesn’t seem to cause the chart to update correctly. I noticed that https://www.shinobicontrols.com/forum/shinobicontrols/2015/10/reuse-of-collectionviewcell-with-shinobichart-inside mentions [chart redrawChart] so I tried that and it appears to fix the drawing. However, that post also mentions that the redrawChart command shouldn’t be needed, so I was wondering if I’m doing something silly when re-using the chart in the cells.

I’ve stuck together a project that illustrates the problem and put it on github: https://github.com/mnem/shinobi-table-test (note that if you check it out you’ll have to create a Licence.h file and #define LICENCE @“whatever” in it, and link to the Shinobi framework). The code is pretty simple. When a cell is dequeued, an array of numbers is passed to it and then the chart in that cell is asked to reload the data.

// ViewController.m whic is the table view data source

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GraphTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[GraphTableViewCell reuseIdentifier] forIndexPath:indexPath];
    
    cell.series = _sections[self.sectionTitles[indexPath.section]][indexPath.row];
    [cell updateChart];
    
    return cell;
}



// GraphTableViewCell.m which is also the chart's data source

- (void)updateChart {
    _label.text = [NSString stringWithFormat:@"Data: %@", [_series componentsJoinedByString:@", "]];
    [_graph reloadData];
}

I’ve set background data loading for the charts to NO in case that was causing odd problems, but it didn’t help.

Any thoughts on what I’m doing wrong?


#2

Hello Mnem,

Thank you for your query. You do need to call the RedrawChart method in order to update the user interface. To fix your problem should be as simple as calling the redrawChart] method just after you call reloadData in your updateChart method. This is because the reloadData method does not automatically force a redraw of the chart.

I hope this helps you.

Matt