Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8411395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:24:42+00:00 2026-06-10T00:24:42+00:00

This is a chat app, I’m using tableview to display the chatting data between

  • 0

This is a chat app, I’m using tableview to display the chatting data between user and others, however I’v got Rows are displayed multiple(duplicate). The code is below:

#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;
    }
}

the print results for each row are:

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>>

Please advise, thanks!

Updated 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:24:43+00:00Added an answer on June 10, 2026 at 12:24 am

    I answered your another question How to calculate the Width and Height of NSString on UILabel

    They are similar problem. the code below I C&P from the other question.

    That’s because you did not reuse the table cell, the structure should be like:

    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)];
    

    I’ve been gone through and answered some of your question, that’s correct way to write your tableview controller. And your problem will be solved.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm working on this site web app that should let users easily chat
(I am developing an app that presents chat messages in a table. But, this
Is this chat using long polling or http streaming ? http://go-mono.com/moonlight/chat.aspx
this is kinda overkill for me.. I am using a JTextPane for a chat,
I'm implementing a simple chat in .NET using Rx on the basis of this
I have used the signalR chat app (as laid out in this tutorial http://sergiotapia.com/2011/09/signalr-with-mvc3-chat-app-build-asynchronous-real-time-persistant-connection-websites/
I'm writing a little chat app, and I have this event handler: void o_Typing(object
My app is a Wifi chat app with which you can communicate between two
am doing chat app in which i want to display photo,adaptermodel,small icon for online
I'm building a simple chat app with Rails. when a user types in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.