Chart reuse in UITableView cell



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. Here the graphs should all be near the top of the Y axis:

I noticed that 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 simple project that illustrates the problem and put it on github: (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?


Greetings Program!

In your case, because you have the graph configured as an IBOutlet and are not instantiating and adding it as a subView of the cell programmatically, you will have to call redrawChart.

What you can also do is move updateChart into layoutSubviews and then you do not need to call [cell updateChart].

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



Interesting. Is it uncommon to add chart controls via Interface Builder?


I can’t say if it’s uncommon but I myself haven’t wired it up as such.

I’ve always added it as a subView of the cell’s contentView but I’ve never needed to have other controls In the cell as you do with the label. But I probably would have just changed the frame to position the chart; although using an IBOutlet does make things easier for positioning with multiple controls.

I use the chart in my iParent Points Tracker app and it’s a static height, in its own section, with the header displaying the title.