0

这是一个聊天应用程序,我使用 tableview 来显示用户和其他人之间的聊天数据,但是我有行显示多个(重复)。代码如下:

#define kMyTag 1

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{   
    NSDictionary *messageDic = [self.chatArray objectAtIndex:indexPath.row];
    if ([[messageDic objectForKey:@"myself"]boolValue] == false) {
        static NSString *CellIdentifier = @"MessageChatFriendCell"; 
        MessageChatFriendCell *cell = (MessageChatFriendCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[MessageChatFriendCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        NSDictionary *chatInfo = [self.chatArray objectAtIndex:[indexPath row]];
        UIView *chatView = [chatInfo objectForKey:@"view"];
        chatView.tag = kMyTag;
        [cell.contentView addSubview:chatView];
        return cell;
    } else  {
        static NSString *CellIdentifier = @"MessageChatSelfCell";
        MessageChatSelfCell *cell = (MessageChatSelfCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[MessageChatSelfCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        NSDictionary *chatInfo = [self.chatArray objectAtIndex:[indexPath row]];
        UIView *chatView = [chatInfo objectForKey:@"view"];
        chatView.tag = kMyTag;
        [cell.contentView addSubview:chatView];
        return cell;
    }
}

每行的打印结果是:

2012-08-02 15:44:47.152  MessageChatSelfCell-><UIView: 0x222790; frame = (85 0; 230 114); tag = 1; layer = <CALayer: 0x222320>>
2012-08-02 15:44:47.166  MessageChatFriendCell-><UIView: 0x21d790; frame = (0 0; 210 132); tag = 1; layer = <CALayer: 0x21d5c0>>
2012-08-02 15:44:47.176  MessageChatFriendCell-><UIView: 0x21bde0; frame = (0 0; 177 60); tag = 1; layer = <CALayer: 0x21be10>>
2012-08-02 15:44:47.183  MessageChatSelfCell-><UIView: 0x216430; frame = (85 0; 230 168); tag = 1; layer = <CALayer: 0x215fc0>>
2012-08-02 15:44:59.232  MessageChatFriendCell-><UIView: 0x215150; frame = (0 0; 86 60); tag = 1; layer = <CALayer: 0x214eb0>>
2012-08-02 15:45:00.465  MessageChatSelfCell-><UIView: 0x213220; frame = (85 0; 230 78); tag = 1; layer = <CALayer: 0x213250>>

请指教,谢谢!

2012-08-08 更新

#define WRITER_NAME_LABEL_TAG 4
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{   
    static NSString *MessageChatSelfCellIdentifier = @"MessageChatSelfCell";    
    static NSString *MessageChatFriendCellIdentifier = @"MessageChatFriendCell";
    UITableViewCell *cell = nil;
    UIView *chatView = nil;
    NSDictionary *messageDic = [self.chatArray objectAtIndex:indexPath.row];
    NSString *text = [messageDic objectForKey:@"compiled"];
    BOOL myMessage = [[messageDic objectForKey:@"myself"] boolValue];

    if (myMessage) {
        cell = [tableView dequeueReusableCellWithIdentifier:MessageChatSelfCellIdentifier];

        if(cell == nil) 
        { 
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                           reuseIdentifier:MessageChatSelfCellIdentifier];

        chatView = [self bubbleView:[NSString stringWithFormat:@"%@", text] 
                                       from:YES];
        chatView.tag = WRITER_NAME_LABEL_TAG;
        NSLog(@"MessageChatFriendCell->%@",chatView);
        [cell addSubview:chatView];
        }else {
            chatView = (UIView *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
        }
    } else  {
        cell = [tableView dequeueReusableCellWithIdentifier:MessageChatFriendCellIdentifier];

        if(cell == nil) 
        { 
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                          reuseIdentifier:MessageChatFriendCellIdentifier];

        NSString *text = [messageDic objectForKey:@"compiled"];        
        UIView *chatView = [self bubbleView:[NSString stringWithFormat:@"%@", text] 
                                       from:NO];
        chatView.tag = WRITER_NAME_LABEL_TAG;
                NSLog(@"MessageChatSelfCell->%@",chatView);
        [cell addSubview:chatView];
        }else {
            chatView = (UIView *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
        }
    }  
        return cell;
}
4

2 回答 2

0

您应该更改的几件事,首先不要将您的视图存储在 chatInfo 中,而是存储您的数据。
接下来仅在您创建新单元格时将视图添加到单元格,否则您将向重用的单元格添加多个视图。
看看下面的例子,这里我正在创建一个带有消息作者姓名的单元格,对于我的消息,作者姓名将在右侧,对于我的朋友消息,它将在左侧。请注意,我只是在创建新单元格时添加作者姓名标签,然后我给它一个标签,以便下次我想更改它时可以获取标签。

#define WRITER_NAME_LABEL_TAG 1

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MessageChatFriendCellIdentifier = @"MessageChatFriendCell";
    static NSString *MessageChatSelfCellIdentifier = @"MessageChatSelfCell";

    UITableViewCell *cell = nil;
    UILabel *writerNameLabel = nil;

    NSDictionary *messageInfo = [self.chatArray objectAtIndex:indexPath.row];
    BOOL myMessage = [[messageInfo objectForKey:@"myself"] boolValue];

    if (myMessage) {        
        // this is my message
        cell = [tableView dequeueReusableCellWithIdentifier:MessageChatSelfCellIdentifier];

        if(cell == nil) 
        { 
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                           reuseIdentifier:MessageChatSelfCellIdentifier] autorelease];

            writerNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
            writerNameLabel.tag = WRITER_NAME_LABEL_TAG;
            writerNameLabel.textAlignment = UITextAlignmentRight;
            [cell addSubview:writerNameLabel];
        }
        else {
            writerNameLabel = (UILabel *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
        }
    }
    else {
        // this is my friend message
        cell = [tableView dequeueReusableCellWithIdentifier:MessageChatFriendCellIdentifier];

        if(cell == nil) 
        { 
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                           reuseIdentifier:MessageChatFriendCellIdentifier] autorelease];

            writerNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
            writerNameLabel.tag = WRITER_NAME_LABEL_TAG;
            writerNameLabel.textAlignment = UITextAlignmentLeft;
            [cell addSubview:writerNameLabel];
        }
        else {
            writerNameLabel = (UILabel *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
        }
    }

    // get the current writer name from the model
    NSString *writerName = [messageInfo objectForKey:@"writer"];        

    // configure cell...
    writerNameLabel.text = writerName;

    return cell;
}
于 2012-08-02T10:41:18.483 回答
0

我回答了你的另一个问题How to calculate the Width and Height of NSString on UILabel

他们是类似的问题。下面的代码是来自另一个问题的 C&P。

那是因为你没有重用表格单元格,结构应该是这样的:

NSString *text = [messageInfo objectForKey:@"compiled"];
if(cell == nil) 
        { 
     writerNameLabel.numberOfLines = 0;
     writerNameLabel.textAlignment = UITextAlignmentRight;
     writerNameLabel.backgroundColor = [UIColor clearColor];
     [cell addSubview:writerNameLabel];
}
else {
     writerNameLabel = (UILabel *)[cell viewWithTag:WRITER_NAME_LABEL_TAG];
}
CGSize constraint = CGSizeMake(296,9999);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] 
               constrainedToSize:constraint 
                   lineBreakMode:UILineBreakModeWordWrap];
[writerNameLabel setFrame:CGRectMake(writerNameLabel.frame.origin.x, writerNameLabel.frame.origin.y, size.width, size.height)];

我已经完成并回答了您的一些问题,这是编写 tableview 控制器的正确方法。你的问题将得到解决。

于 2012-08-20T06:29:29.580 回答