我正在为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];
}发布于 2011-06-23 00:42:52
我猜在这个阶段你有两个图像。左气泡图像和右气泡图像,这是可以添加图像到层。假设您有两个单元格,一个指向左边,另一个指向右边,您必须对TableCell进行子类化。定义一个自定义样式,根据指定的样式值,您可以将这些图像添加到您已添加(非法添加)的背景视图的子层。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
https://stackoverflow.com/questions/6441761
复制相似问题