0

我正在编写一个 IOS 聊天应用程序。我有一个表格视图,其中每个单元格都包含一个文本框,加载每个单元格后,我订阅了 pubnub.com 上的聊天频道。我在 viewdidLoad 中有一个 observable 来监视传入的消息。从 observable 接收到的对象包含通道名称以及消息文本和日期。我想向其相应的单元格显示消息。我不确定在查看并订阅频道时在哪里捕获满载的单元格。那么在可观察的情况下,我如何将频道名称与屏幕上当前显示的单元格进行比较?我尝试了 isVisible 但我得到的不仅仅是屏幕上可见的内容。问题是我只想向当前可见的单元格显示消息,当用户停止在该单元格上时,即使他们不点击它,葡萄树如何开始播放视频。

请参阅下面的代码

- (void)viewDidLoad
{
    [super viewDidLoad];

    appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    self.messages = [NSMutableDictionary dictionary];
    self.configuration = [PNConfiguration defaultConfiguration];

    [self load_DEMO_DATA];
    [self setClient];
    [self connectToServer];

    //Observable
    [[PNObservationCenter defaultCenter] addMessageReceiveObserver:self
                                                         withBlock:^(PNMessage *message) {

        NSDateFormatter *dateFormatter = [NSDateFormatter new];
        dateFormatter.dateFormat = @"HH:mm:ss MM/dd/yy";
        PNChannel *channel = message.channel;

        NSString *messages = [self.messages valueForKey:channel.name];
        if (messages == nil) {messages = @"";}
        messages = [messages stringByAppendingFormat:@"<%@> %@\n",[dateFormatter stringFromDate:message.receiveDate.date],message.message];

        //Get TextBox & Set Caption                                                                                    
        UITextView *caption = (UITextView *)[[(UITableViewCell *)[(UITableView    *)self.tableView cellForRowAtIndexPath:CurrentIndexPath] contentView] viewWithTag:105];

        caption.text = [NSString stringWithFormat:@"%@%@", caption.text, messages];
        [caption scrollRangeToVisible:NSMakeRange([caption.text length], 0)];                         
    }];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TimelinePostCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell==nil)
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    // Configure the cell...
    NSDictionary *post = [posts objectAtIndex:indexPath.item];
    NSDictionary *user = [post objectForKey:@"user"];

    //Set Current Channel
    self.currentChannel = [PNChannel channelWithName:[post objectForKey:@"channelName"]      shouldObservePresence:YES];

    //Subscribe to Chat
    [self subscribeToChannel:self.currentChannel.name];
    self.currentPost = post;

    //Get Channel History
    [self ChannelHistory];

    return cell;
}
4

1 回答 1

1

好吧,首先,-tableView:cellForRowAtIndexPath:不应该用于启动任何耗时的操作。为了保持高性能,您应该在 160 毫秒内从该方法返回准备好的UITableViewCell ,否则您将看到"lag"。此方法将在表格显示后立即调用几次(与具有值的单元格一样多)。

当您应该使用PubNub客户端启动频道订阅和任何其他操作时,您应该使用–scrollViewDidEndDragging:willDecelerate:(使用decelerate NO)以及适当的地点和时间。–scrollViewDidEndDecelerating:

您可以一次订阅所有频道 - 与逐个订阅每个频道相比,这将减少网络开销。如果您想通过让客户在少数频道上订阅来保留资源并保持低价,那么您应该使用相同的方法取消订阅以前的频道(与检测当前单元格和存储当前频道等的建议相同)。

也只是关于如何用模型喂养单元格的建议:在自定义单元格类中移动模型处理(控制器没有理由了解单元格视图的结构以及应该在那里显示哪些数据)。

于 2013-11-18T23:33:02.343 回答