Q
Q
quard2013-02-23 23:04:40
iOS
quard, 2013-02-23 23:04:40

UIImageView in UITableView "flies" from left to right?

I do something like a chat. I need to draw beautiful "clouds" like in the Messages application. Everything would be fine, but when previously invisible cells appear on the screen, the background flies from left to right.
At first I thought that the frame was set after adding the view, but it turned out that everything was ok.
Table code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    IKChatMessageData *data = [_messages objectAtIndex:indexPath.row];  
    IKChatMessageCell *cell = [[IKChatMessageCell alloc] init];
  
    [cell setData:data];
    
    return cell;
}

SetData code for IKChatMessageCell:
- (void)setData:(IKChatMessageData *)data
{
    _data = data;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    IKChatMessageType type = _data.type;
    
    CGFloat width = _data.view.frame.size.width;
    CGFloat height = _data.view.frame.size.height;
    
    CGFloat x = (type == MessageTypeOther) ? 0 : self.frame.size.width - width - self.data.insets.left - self.data.insets.right;
    CGFloat y = 0;
    
    if (!self.backgroundImage)
    {
        self.backgroundImage = [[UIImageView alloc] init];
    }
    
    if (type == MessageTypeOther)
    {
        self.backgroundImage.image = [[UIImage imageNamed:@"bubbleSomeone.png"] stretchableImageWithLeftCapWidth:21 topCapHeight:14];
        
    }
    else {
        self.backgroundImage.image = [[UIImage imageNamed:@"bubbleMine.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:14];
    }
    
    self.backgroundView.frame = CGRectMake(x, y, width + self.data.insets.left + self.data.insets.right, height + self.data.insets.top + self.data.insets.bottom);

    
    _data.view.frame = CGRectMake(x + self.data.insets.left, y + self.data.insets.top, width, height);
    [_data.view removeFromSuperview];

    [self addSubview:self.backgroundImage];
    [self addSubview:_data.view];
}

These are pieces of rewritten code from UIBubbleTableView, this problem is also there, but after rewriting, nothing helped.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
An, 2013-02-24
@quard

There are a lot of errors in this code, starting with the initialization
of IKChatMessageCell *cell = [[IKChatMessageCell alloc] init]; //will result in out-of-memory
AND ending with
CGFloat x = (type == MessageTypeOther)? 0: self.frame.size.width - width - self.data.insets.left - self.data.insets.right; // what self.frame.size.width will be equal to is not clear at this moment
My advice to you is to find a normal example with bubbles (there are actually a lot of them) and just use it.

A
An, 2013-02-24
@Flanker_4

1) There can be a lot of messages in a chat. In addition, in each cell you have a UIImageView with a bubble picture (although the UIImage may be cached) far from 10x10 pixels (+ retina). And the application can also be launched after some kind of iOS farcry, when the system will have very little free memory. It's best to use reuse where possible. Anyway.
2) Yes, it will, but only after the cell becomes part of the table (and this will definitely happen after the return in the delegate method, if there is no magic in the cell's init). And at the setDate stage, it's just a cell that exists in memory. Now there are probably no problems due to the fact that you have autoresizingMask set to stretch. Try moving the code responsible for forming the size of the bubble to the willDisplay delegate method, maybe this will solve the problem.
But better heed my previous advice.
There is another option that you post an example project that reproduces the problem, because now too much is hidden: among other things, it is not clear how the data is formed, and there are a lot of size values. But I don't really want to poke around in your code :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question