Question

I am trying to create simple messaging app just like the built one.

I want to reproduce the same effect speech bubbles as iMessage.

I found a project from apple called MultipeerGroupChat which has that functionality.

The problem is It has a lot more than what I need making it hard to replicate, because of class dependencies. I dont need multipeer or sending images.I stripped a lot of code out already.

I now have a simple TableView, I added the bubble images and 2 classes:

  • MessageView.h
  • Transcript.h

I narrowed down the issue to this table view delegate to display the bubbles:

// The individual cells depend on the type of Transcript at a given row.  We have 3 row types (i.e. 3 custom cells) for text string messages, resource transfer progress, and completed image resources
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Get the transcript for this row
    Transcript *transcript = [self.transcripts objectAtIndex:indexPath.row];

    // Check if it's an image progress, completed image, or text message
    UITableViewCell *cell;
    if (nil != transcript.imageUrl) {
        // It's a completed image
        cell = [tableView dequeueReusableCellWithIdentifier:@"Image Cell" forIndexPath:indexPath];
        // Get the image view
        ImageView *imageView = (ImageView *)[cell viewWithTag:IMAGE_VIEW_TAG];
        // Set up the image view for this transcript
        imageView.transcript = transcript;
    }
    else if (nil != transcript.progress) {
        // It's a resource transfer in progress
        cell = [tableView dequeueReusableCellWithIdentifier:@"Progress Cell" forIndexPath:indexPath];
        ProgressView *progressView = (ProgressView *)[cell viewWithTag:PROGRESS_VIEW_TAG];
        // Set up the progress view for this transcript
        progressView.transcript = transcript;
    }
    else {
        // Get the associated cell type for messages
        cell = [tableView dequeueReusableCellWithIdentifier:@"Message Cell" forIndexPath:indexPath];
        // Get the message view
        MessageView *messageView = (MessageView *)[cell viewWithTag:MESSAGE_VIEW_TAG];
        // Set up the message view for this transcript
        messageView.transcript = transcript;
    }
    return cell;
}

As mention before I only need the message so I stripped down to this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
  UITableViewCell *cell;
  Transcript *transcript = [self.messageArray objectAtIndex :[indexPath row]];            
  cell = [tableView dequeueReusableCellWithIdentifier:@"Message Cell" forIndexPath:indexPath];
  MessageView *messageView = (MessageView *)[cell viewWithTag:MESSAGE_VIEW_TAG];
  messageView.transcript = transcript;    
  //how does the code add the view and return it ?? :-S     

  return cell;
}

This code does not display anything.

Now I dont understand how this code customize the cell to show speech bubbles.

Please advice.

Was it helpful?

Solution

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath on UITableView will return an instance of UITableViewCell that is ready to be reused or if no reusable cells exist it will create one.

However, according to the documentation, you need to first register a nib or Class with the table view so it knows what cell maps to that reuse identifier.

Check out the methods:
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier

OTHER TIPS

You are missing:

[cell.contentView addSubview:messageView];

in:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

It should be:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
  UITableViewCell *cell;
  Transcript *transcript = [self.messageArray objectAtIndex :[indexPath row]];            
  cell = [tableView dequeueReusableCellWithIdentifier:@"Message Cell" forIndexPath:indexPath];
  MessageView *messageView = (MessageView *)[cell viewWithTag:MESSAGE_VIEW_TAG];
  messageView.transcript = transcript;    

    [cell.contentView addSubview:messageView];

  return cell;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top