需要帮助理解简单消息传递示例应用

时间:2014-02-19 22:09:28

标签: ios objective-c messaging imessage

我正在尝试创建简单的消息传递应用程序,就像构建它一样。

我想重现与iMessage相同的效果气泡。

我发现了一个名为MultipeerGroupChat的苹果项目,该项目具有该功能。

问题是,由于类依赖性,它比我需要的更多,因此难以复制。我不需要多种颜色或发送图像。我已经剥离了很多代码。

我现在有一个简单的TableView,我添加了气泡图像和2个类:

  • MessageView.h
  • Transcript.h

我将问题缩小到此表视​​图委托以显示气泡:

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

如前所述,我只需要这条消息,所以我剥离了这个:

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

此代码不显示任何内容。

现在我不明白这段代码如何自定义单元格以显示气泡。

请建议。

2 个答案:

答案 0 :(得分:0)

你错过了:

[cell.contentView addSubview:messageView];

在:

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

应该是:

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

答案 1 :(得分:0)

UITableView上的

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath将返回一个可以重用的UITableViewCell实例,或者如果不存在可重用的单元格,它将创建一个。

但是,根据文档,您需要首先使用表视图注册一个nib或Class,以便它知道哪个单元映射到该重用标识符。

检查方法:
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier

相关问题