2010-11-08 44 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