2015-08-28 101 views
0

我使用自定義UITableViewCell和界面生成器在我的tableview上創建了一些標籤。現在我使用了一些名爲BEMLineGraph的第三方控件,我想用代碼將它添加到我的tableview單元格中。它也有一些代表和數據源方法。我正在做下面的事情,但問題是我上下滾動時會得到重複的圖形和亂七八糟的數據。使用initWithStyle以編程方式定製UITableViewCell

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; 
} 

回答

1

一件事錯了,我可以看到你的代碼:

if (cell == nil) 
    { 
     cell = [cell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
    } 

它應該是:

if (cell == nil) 
{ 
    cell = [[ProductsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
} 

但正如你已經子類反正細胞這不會解決副本圖形

的問題,你爲什麼不保持productGraph的委託和數據源的細胞本身

+0

我編輯了我的代碼,它好像是單元格從來沒有零!關於委託和數據源,我需要在TableViewController.m中調用它們,因爲我在那裏做了一些數據準備和核心數據獲取。 – user3687

+0

保留if語句,這很重要......你的情況發生了什麼是你每次滾動時都將子視圖添加到單元格,因此你可能會看到重複的圖表 – Jatin

+0

爲什麼我應該? if語句沒有出現。聲明(cell == nil)始終爲false。 – user3687

1

如果您已將ProductsTableViewCell類註冊爲UITableView,那麼dequeueReusableCellWithIdentifier:將爲您創建該類的對象,並調用其initWithCoder:方法(用於在Interface Builder中定義的單元格)。因此,請取消對initWithStyle:reuseIdentifier:的呼叫,並使用initWithCoder:方法ProductsTableViewCell進行初始化。

然後,在你的cellForRowAtIndexPath:只做那個細胞特有的東西。這取決於Graph類的實現,但它需要確保舊圖不再可見。

+0

如果我刪除initWithStyle:reuseIdentifier:我應該如何配置圖表? – user3687

+0

在類的init:方法中進行通用初始化,並在cellForRowAtIndexPath中對單元格進行特定的配置: – fishinear

+0

好的感謝您的幫助。讓我再解釋一下情況。我使用自定義的uitableviewcell創建了我的單元格。每個單元格上的標籤和圖像視圖都是使用界面構建器創建的。但我使用的圖只能以編程方式添加(或至少多數民衆贊成我認爲)。該圖還有一些代表和數據源。現在我無法正確設置圖形。圖形重疊在一起。 – user3687

相關問題