我有一个包含图表的数组,我希望在表格视图中显示。由于绘制图表需要几毫秒,这会使滚动起伏不定,我想从重新使用的单元格的cell.contentView中删除旧图表,并在滚动时添加带有正确图表的新子视图(请参阅下面的源代码)。这有效:图表正确显示。
然而,在添加子视图时,使用的内存增加并且第一次向下滚动是不稳定的(在所有行显示一次之后滚动不起伏)。
似乎已经存储在实例变量(strong)中的图表数据被复制(不仅仅是引用)到UITableViewCell。
我想避免这种情况,以便减少使用内存。
总结:如何在使用addSubview时避免将我的图表复制到UITableViewCell中。相反,我想在使用addSubview时只添加对数据的引用。
static NSString *CellIdentifier = @"chart";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
{
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
VMKChartData *chartData = [shinobiCharts_ objectAtIndex:rowNumber];
ShinobiChart *shinobiChart = chartData.shinobiChart;
[cell.contentView addSubview:shinobiChart];
[self setSeparatorInsets:cell];
return cell;
答案 0 :(得分:0)
不要使用普通UITableViewCell
和addSubview:
,但要在@property (nonatomic, weak) UIView *chartView
中创建一个具有属性tableView:cellForRowAtIndexPath:
的自定义表视图子类,只需分配它即可。
cell.chartView = shinobiChart;
在chartView的setter中,将其添加到内容视图中。
-(void)setChartView:(UIView *)chartView
{
[_chartView removeFromSuperview];
_chartView = chartView;
[self.contentView addSubView:chartView];
}