0

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

我想重现与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;
}

此代码不显示任何内容。

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

请指教。

4

2 回答 2

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;
}
于 2014-02-19T22:14:36.917 回答
0

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

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

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

于 2014-02-19T22:21:21.750 回答