我正在编写一个 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;
}