0

I am implementing chat application and I want to change width of table view cell in chat application so that sender can see his messages left aligned and receiver can see his messages right aligned.

code as below:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
chatCell *cell = (chatCell *)[tableView dequeueReusableCellWithIdentifier:CHAT_CELL_IDENTIFIER];
NSUInteger row = indexPath.row;
if (row < chatData.count)
{
    self.bubbleImage = [[UIImageView alloc] init];
    self.bubbleImage.frame = CGRectMake(0,22,250,66);
    self.bubbleImage.image = [[UIImage imageNamed:@"bubbleMine.png"] stretchableImageWithLeftCapWidth:21 topCapHeight:14];

    NSString *chatText = [[chatData objectAtIndex:row] objectForKey:TEXT];
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    UIFont *font = [UIFont systemFontOfSize:14];
    CGSize size = [chatText sizeWithFont:font constrainedToSize:CGSizeMake(150.0f, 1000.0f) lineBreakMode:NSLineBreakByCharWrapping];
    cell.textString.frame = CGRectMake(75, 18, size.width +20, size.height + 20); // set text frame
    cell.textString.font = [UIFont fontWithName:FONT_NAME size:FONT_SIZE];        // set text font
    cell.textString.text = chatText;                                              // set text
    [cell.textString sizeToFit];

    NSDate *theDate = [[chatData objectAtIndex:row] objectForKey:DATE];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:DATE_FORMAT];
    NSString *timeString = [formatter stringFromDate:theDate];
    cell.timeLabel.text = timeString;                                       // set timeLabel to display date and time
    cell.userLabel.text = [[chatData objectAtIndex:row] objectForKey:NAME]; // set userLabel to display userName

    [self.bubbleImage addSubview:cell.userLabel];
    [self.bubbleImage addSubview:cell.timeLabel];
    [self.bubbleImage addSubview:cell.textString];

    [cell addSubview:self.bubbleImage];

}
return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = [[chatData objectAtIndex:indexPath.row] objectForKey:TEXT];
UIFont *cellFont = [UIFont fontWithName:FONT_NAME size:FONT_SIZE];
CGSize constraintSize = CGSizeMake(225.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
return labelSize.height + 40;
}
4

1 に答える 1

1

セル内で右揃えと左揃えを切り替えることができるように、ユーザーを特定するのに役立つものを保持してください。その情報を使用して、それに応じて画像(右揃えと左揃え)を配置できます。セルの幅のサイズを変更する必要はありません。むしろ、テキストのサイズを処理する必要があり、チャットで特定のセルの行の高さを調整する必要があります。

于 2013-05-30T10:12:48.093 に答える