开发者

UITableViewCell balloons like iChat

开发者 https://www.devze.com 2023-03-14 21:10 出处:网络
I\'m doing something like a chat app for iPhone, and I found this piece of code from Sam Soffes called SSMessagesViewController What I found is that it works perfect until my table has many rows, then

I'm doing something like a chat app for iPhone, and I found this piece of code from Sam Soffes called SSMessagesViewController What I found is that it works perfect until my table has many rows, then I supose that drawrect is reusing cells because is repeating same row content every 6 (but data is different)... any idea on how I can resolve that?

- (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:kLin开发者_C百科eBreakMode alignment:(_messageStyle == SSMessageStyleRight) ? UITextAlignmentRight : UITextAlignmentLeft];
}


I guess at this stage you are having two images. Left bubble image and right bubble image, Its possible to add images into layers. Imagine you have two cells one left pointed and another one right pointed You have to Subclass the TableCell. Define a custom style and, depending on style value assigned you can add these images to the sublayer of Backround view you have added(exlicitly added). http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/ProvidingCALayerContent.html Also do one thing. override the method layoutsubviews. CALayers didn't get resized on its UIView's bounds change. Why? its an example. At this point reset the size of the sublayer of BackgroundView to the size of table cell. Now you have to just sepcify the Tablecellheight in delgate method http://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006942-CH3-SW25

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号