2010-11-08 46 views
0

我在UITableView中绘制自定义视图有一个时髦的问题。我的自定义视图的drawRect函数(称为ChatBubbleView)仅绘制了一个常量大小的矩形。UITableView在5个单元格后失败的自定义绘图

我在ChatBubbleView的drawRect函数中有一个NSLog调试语句。前5次我给UITableView添加一个单元格,所有东西都很好地绘制,并且我看到了NSLog语句触发器。然而,5之后,绘图变得乱码,我不再看到调试声明。

我完全不知所措,我很感激您可能提供的任何帮助。提前致谢!

这是在UITableView上调用reloadData时调用的函数。

- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (forumMode) { 
    //This part works, ignore 

    } else { 

     ChatBubbleView* chatBubble = nil; 

     if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
             reuseIdentifier:CellIdentifier] autorelease]; 

      // Add the bubble 
      chatBubble = [[[ChatBubbleView alloc] init] autorelease]; 
      chatBubble.tag = BUBBLEVIEW_TAG; 
      [cell.contentView addSubview:chatBubble]; 

     } else {   

      // Reuse old views   
      chatBubble = (ChatBubbleView*)[cell.contentView viewWithTag: BUBBLEVIEW_TAG];   

      } 

     chatBubble.labelSize = CGSizeMake(50, 18); 
     chatBubble.frame = CGRectMake(0, 0, chatBubble.labelSize.width, chatBubble.labelSize.height); 

    } 

    return cell; 
} 

编辑:我要补充一点,即重新加载UITableView的功能如下:

-(IBAction) btnAdd:(id) sender { 
    [listOfMessages addObject:textView.text]; 

    [self.tableView reloadData]; 
} 

编辑2:我发现,在细胞中的自定义视图有时重绘自己,当我滚动细胞进出视野。看起来,tableview刷新改变了自定义视图的参数。

回答

0

你说的//This part works, ignore但是如果你重复使用CellIdentifier,它可能无法工作,因为将被出队的单元可能是不同类型的单元(它碰巧具有相同的CellIdentifier)。确保每个单元格类型都有唯一的CellIdentifier。

可以肯定,把一些调试你的if/else块,看你要什么细胞回...

+0

我一定会做到这一点,谢谢。 BOOL值forumMode在viewDidLoad函数中设置为false,并且永远不会更改,所以我知道if(forumMode)语句永远不会计算为true。这就是为什么我把忽略评论放在那里。 – CaptainStiggz 2010-11-08 16:51:55