iOS - drawRect:调用两次

时间:2014-09-03 01:07:32

标签: ios objective-c uitableview uiview drawrect

我是iOS的新手,我读过类似的问题,但没有一个答案有效。 我有一个自定义单元格的桌面视图,显示图形(自定义UIView)和过去的足球比赛。不在同一个单元格中,但它就像图形单元格(BCCGraphCell) - >相应的匹配(BCCPastMatchCell),graph-cell - >相应的比赛..

我正在使用Storyboards,在我的自定义单元格中,我添加了一个图形子视图(BCCGraphView)。奥特莱斯还可以。我重写drawRect:方法来绘制图形,但它只被调用两次。我在这里错过了什么?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifierPastMatchCell = @"pastMatchCell";
    static NSString *cellIdentifierGraphCell = @"graphCell";
    if (indexPath.section % 2 == 0) {
        BCCGraphCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifierGraphCell];
        if (!cell) {
            cell = [[BCCGraphCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierGraphCell];
        }
        return cell;
    }
    BCCPastMatchCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifierPastMatchCell];
    if (!cell) {
        cell = [[BCCPastMatchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierPastMatchCell];
    }
    BCCMatch *matchForCell = [self.pastMatches objectForKey:self.pastMatchesKeyNames[indexPath.section]][indexPath.row];

    [self configurePastMatchCell:cell];
    return cell;
}

- (void)drawRect:(CGRect)rect
{
    self.numberOfMatches = 6; // It's suppose to affect the 6 past match cells
    CGContextRef context = UIGraphicsGetCurrentContext();
    // **** Grid thikckness and color ****
    CGContextSetLineWidth(context, [BCCUIConstant kGraphGridThickness]);
    CGContextSetStrokeColorWithColor(context, [[BCCUIConstant colorForViewGraphGrid] CGColor]);

    // Draw the Xlines
    for (int i = 0; i <= [self numberOfGridLinesX]; i++) {
        [self setNeedsDisplay];

        CGContextMoveToPoint(context, [self offsetX] + i * [self stepX], kGraphTop);
        CGContextAddLineToPoint(context, [self offsetX] + i * [self stepX], kGraphBottom);
    }
    // Draw the Ylines
    for (int i = 0; i < [self numberOfGridLinesY]; i++) {
        [self setNeedsDisplay];

        CGContextMoveToPoint(context, 0, kGraphBottom - [self offsetY] - i * [self stepY]);
        CGContextAddLineToPoint(context, kGraphWidth, kGraphBottom - [self offsetY] - i * [self stepY]);
    }

    // Commit drawing
    CGContextStrokePath(context);
}

1 个答案:

答案 0 :(得分:-1)

删除drawRect中的setNeedsDisplay

[tableView reloadData];
[self setNeedsDisplay];