2014-01-21 159 views
0

我創建一個UIView(實際上是在內容查看子視圖中的一個),它繪製統計內繪製的UIView。自定義的UITableViewCell

這種觀點是在一個UITableView的每個小區設置。

我配置的電池,在:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellConstant = @"constant"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellConstant forIndexPath:indexPath]; 

    SCHGraphicView *statView = (SCHGraphicView *)[cell viewWithTag:TAG_STAT_GRAPH]; 
    [statView setStatValues:[self.listOfListWithValues objectAtIndex:indexPath.row] andBase:10 andColorBar:[UIColor whiteColor]];  

    return cell; 
} 

這是drawRect方法,它現在處於測試模式我畫只是一個值。

- (void)drawRect:(CGRect)rect 
{ 
    CGFloat heightCalculate = ([(NSNumber *)[self.statValues objectAtIndex:1]intValue] *self.frame.size.height)/self.base; 
    //We create the path 
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0,self.frame.size.height - heightCalculate, self.frame.size.width, heightCalculate)]; 
    path.lineWidth = 0; 
    [self.barColor setFill]; 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    //We saved the context 
    CGContextSaveGState(context); 



    [path fill]; 
    //Restore the contex 
    CGContextRestoreGState(context); 
} 

Althought我cellForRowAtIndexPath:每一次修改的統計值,這些值不來電[UIView的setNeedDisplay]所以我不每次重繪由於統計在每個小區從來沒有改變,但不同的是統計如果您比較單元格之間的統計信息。

問題是,當我滾動,我回來第一個單元格,這不包含最初得出的統計。

我實現這一目標的唯一方法是每次重繪cellForRowAtIndexPath 中的統計信息,但我認爲預覽是在第一次繪製視圖時保存的,並且用於提高性能,如果您打電話給[UIView setNeedDisplay]視圖再次重繪。

有什麼想法?

非常感謝

回答

1

貫徹落實UITableViewCelldrawRect:當我經歷了奇怪的問題,看來你不應該這樣做。您應該創建您的UIView並將其添加爲contentView屬性的子視圖。有很多問題/答案就在這裏和整個網絡約drawRect:UITableViewCell,其中大部分建議大家不這樣做。

subclassed UITableViewCell - backgroundView covers up anything I do in drawRect

+0

是的,我做到了,我創造了一個視圖。其實是contentView中的子視圖之一。感謝您的回答 :) – xarly