Я создал несколько меток в своем табличном представлении, используя пользовательский UITableViewCell и построитель интерфейсов. Теперь я использую какой-то сторонний элемент управления BEMLineGraph, и я хочу добавить его в ячейку tableview с помощью кода. Он также имеет несколько делегатов и методы источников данных. Я делаю следующее, но проблема в том, что я получаю дубликаты графиков и перепутал данные при прокрутке вверх и вниз.Программируемый пользовательский UITableViewCell с использованием initWithStyle
ProductsTableViewCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.productGraph = [[BEMSimpleLineGraphView alloc] initWithFrame:self.graphContainter.frame];
//_myGraph.enableTouchReport = YES;
self.productGraph.tag = 100;
self.productGraph.animationGraphStyle = BEMLineAnimationNone;
//_myGraph.enablePopUpReport = YES;
self.productGraph.enableXAxisLabel = YES;
self.productGraph.colorTouchInputLine = [UIColor whiteColor];
self.productGraph.colorXaxisLabel = [UIColor darkGrayColor];
self.productGraph.colorTop = [UIColor clearColor];
self.productGraph.colorBottom = [UIColor clearColor];
self.productGraph.colorLine = [UIColor colorWithRed:255.0/255.0 green:255.0/102.0 blue:255.0/102.0 alpha:1];
self.productGraph.colorPoint = [UIColor lightGrayColor];
[self addSubview:self.productGraph];
}
return self;
}
TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ProductsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell = [cell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.productGraph.frame = CGRectMake(0, 0, cell.graphContainter.frame.size.width, cell.graphContainter.frame.size.height);
cell.productGraph.dataSource = self;
cell.productGraph.delegate = self;
//All the other stuff is set here and works well.
}
- (NSInteger)numberOfPointsInLineGraph:(BEMSimpleLineGraphView *)graph
{
if (graph.tag == 100)
{
return productDetail.count;
}
else
{
return numbers.count;
}
Я отредактировал мой код, кажется, что ячейка никогда не ноль! Что касается делегата и источника данных, мне нужно вызвать их внутри TableViewController.m, так как я делаю некоторые подготовки данных и извлечение основных данных там. – user3687
сохраните инструкцию if, это важно ... Что происходит в вашем случае, так это то, что вы добавляете subview в ячейку при каждом прокрутке, следовательно, вы можете видеть повторяющиеся графики – Jatin
Почему я должен? С утверждением if ничего не появляется. Утверждение (cell == nil) всегда ложно. – user3687