gpt4 book ai didi

ios - observables 和 UItableview 可见单元格

转载 作者:行者123 更新时间:2023-11-29 03:29:05 25 4
gpt4 key购买 nike

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

参见下面的代码

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

最佳答案

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

您应该根据情况使用 –scrollViewDidEndDragging:willDecelerate: (decelerate NO)和 –scrollViewDidEndDecelerating:您应该使用 PubNub 客户端发起 channel 订阅和任何其他操作的地点和时间。

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

另外,只是关于如何向单元格提供模型的建议:将模型处理移至自定义单元格类内( Controller 没有理由知道有关单元格 View 的结构以及应在其中显示哪些数据)。

关于ios - observables 和 UItableview 可见单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20059182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com