我正在尝试创建简单的消息传递应用程序,就像内置的一样。
我想重现与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;
}
此代码不显示任何内容。
现在我不明白这段代码如何自定义单元格以显示气泡。
请指教。