2011-06-22 46 views
0

我正在做一些像iPhone的聊天應用程序,我發現這從Sam Soffes代碼稱爲SSMessagesViewController我發現它是完美的,直到我的表有很多行,然後我supose drawrect正在重用單元格,因爲每6(但數據不同)重複相同的行內容...有關我如何解決這個問題的任何想法?UITableViewCell氣球像iChat

- (void)drawRect:(CGRect)frame { 
    UIImage *bubbleImage = _messageStyle == SSMessageStyleLeft ? _leftBackgroundImage : _rightBackgroundImage; 
    CGSize bubbleSize = [[self class] bubbleSizeForText:_messageText]; 
    CGRect bubbleFrame = CGRectMake((_messageStyle == SSMessageStyleRight ? self.frame.size.width - bubbleSize.width : 0.0f), kMarginTop, bubbleSize.width, bubbleSize.height); 
    [bubbleImage drawInRect:bubbleFrame]; 

    CGSize textSize = [[self class] textSizeForText:_messageText]; 
    CGFloat textX = (CGFloat)bubbleImage.leftCapWidth - 3.0f + ((_messageStyle == SSMessageStyleRight) ? bubbleFrame.origin.x : 0.0f); 
    CGRect textFrame = CGRectMake(textX, kPaddingTop + kMarginTop, textSize.width, textSize.height); 
    [_messageText drawInRect:textFrame withFont:kFont lineBreakMode:kLineBreakMode alignment:(_messageStyle == SSMessageStyleRight) ? UITextAlignmentRight : UITextAlignmentLeft]; 
} 

回答

0

我想在這個階段你有兩個圖像。左側的氣泡圖像和右側的氣泡圖像,可以將圖像添加到圖層中。 想象一下你有兩個單元格左一個指向另一個右指向你必須SubClass TableCell。定義一個自定義樣式,根據指定的樣式值,可以將這些圖像添加到已添加(已明確添加)的Backround視圖的子圖層中。 http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/ProvidingCALayerContent.html 也做一件事。重寫方法layoutsubviews。其中一個例子是CALayers didn't get resized on its UIView's bounds change. Why?。 此時,將BackgroundView的子圖層大小重置爲表格單元格的大小。現在你只需要用delgate方法來清理Tablecellheight http://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006942-CH3-SW25