UITableViewCell像iChat一样气球

时间:2011-06-22 14:40:08

标签: ios uitableview drawrect

我正在做类似iPhone的聊天应用程序,我发现这个来自Sam Soffes的代码叫做SSMessagesViewController我发现它工作得很完美,直到我的表有很多行,然后我认为drawrect是重用单元格因为每6个重复相同的行内容(但数据不同)...有关如何解决这个问题的任何想法?

- (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];
}

1 个答案:

答案 0 :(得分:0)

我想在这个阶段你有两张照片。左气泡图像和右气泡图像,可以将图像添加到图层中。               想象一下,你有两个单元格左指向另一个单元右指向你必须对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方法http://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006942-CH3-SW25

中分离Tablecellheight
相关问题