我在我的应用程序中集成了 Comet Chat 功能。我正在为 Group 和 OneOnOne 聊天使用自定义 UI。一切运行良好,但问题是当我进入群聊屏幕时,我的应用程序变得无响应。当我滚动表格时它变得无响应。
我在CellforRowatIndexPath中放置了调度功能,用于获取图像链接并显示它们。为此,我正在使用SdWebImage框架。
这是我的代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"chatroomchatviewcell";
chatViewCell = [chatTable dequeueReusableCellWithIdentifier:cellIdentifier];
if (chatViewCell == nil) {
chatViewCell = [[ChatRoomChatViewCellSDK alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
else {
dispatch_queue_t concurrentQueue1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(concurrentQueue1, ^{
for (UIView *view in chatViewCell.contentView.subviews) {
[view removeFromSuperview];
}
});
}
/* Define maximum contraints */
CGSize constraints = CGSizeMake(((self.view.frame.size.width)*2/3 + 8.0f),100000);
CGFloat wrapperViewX = 0.0f;
CGFloat wrapperViewWidth = 0.f;
CGFloat wrapperViewHeight = 0.f;
NSString *messageString = nil;
if ([[[messageArray objectAtIndex:indexPath.row] objectForKey:MESSAGE] isKindOfClass:[NSString class]]) {
messageString = [NSString stringWithFormat:@"%@",[[messageArray objectAtIndex:indexPath.row] objectForKey:MESSAGE]];
}
NSString *nameString = nil;
NSString *messageType = [NSString stringWithFormat:@"%@",[[messageArray objectAtIndex:indexPath.row] objectForKey:MESSAGE_TYPE_KEY]];
UIView *wrapperView = [UIView new];
UITextView *textView = [UITextView new];
UIImageView *imageView = [UIImageView new];
UILabel *nameLabel = [UILabel new];
UILabel *timeLabel = [UILabel new];
wrapperView.layer.cornerRadius = 5 ;
wrapperView.clipsToBounds = YES;
/* If message is sent by you add Me as the name */
if ([[NSString stringWithFormat:@"%@",[[messageArray objectAtIndex:indexPath.row] objectForKey:@"fromid"]] isEqualToString:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:LOGGED_IN_USER]]]) {
nameString = @"Me:";
} else {
/* Else append BuddyName in message */
nameString = [NSString stringWithFormat:@"%@:",[[[messageArray objectAtIndex:indexPath.row] objectForKey:FROM] capitalizedString]];
}
[nameLabel setFont:[UIFont systemFontOfSize:14.f]];
[nameLabel setNumberOfLines:0];
[nameLabel setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary *attributesName = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont systemFontOfSize:14.f], NSFontAttributeName, nil];
CGRect nameRect = [nameString boundingRectWithSize:constraints options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributesName context:nil];
CGSize sizeName = nameRect.size;
[nameLabel setText:nameString];
attributesName = nil;
[nameLabel setFrame:CGRectMake(4.f, 4.f, sizeName.width, sizeName.height)];
[wrapperView addSubview:nameLabel];
if ([messageType isEqualToString:MESSAGE_TYPE_IMAGE]) {
UIButton *btnclk = [UIButton new];
btnclk.frame=CGRectMake(4.f, 4.f, 100, 100);
[btnclk setTitle:@"" forState:UIControlStateNormal];
btnclk.tag=indexPath.row;
[btnclk addTarget:self action:@selector(ShowFullImageVIew:)
forControlEvents:UIControlEventTouchUpInside];
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
//this will start the image loading in bg
CGRect imageRect = CGRectMake(4.f, (nameLabel.frame.origin.y + nameLabel.frame.size.height + 4.f), 100.f, 100.f);
[imageView setFrame:imageRect];
dispatch_async(concurrentQueue, ^{
NSURL *imgUrl = [NSURL URLWithString:messageString];
//this will set the image when loading is finished
dispatch_async(dispatch_get_main_queue(), ^{
if (messageString == nil) {
[imageView setImage:[UIImage imageWithData:[[messageArray objectAtIndex:indexPath.row] objectForKey:MESSAGE]]];
} else {
if ([[NSFileManager defaultManager] fileExistsAtPath:messageString]) {
[imageView setImage:[UIImage imageWithContentsOfFile:messageString]];
} else {
UIActivityIndicatorView *activityIndicator;
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.color = [UIColor redColor];
activityIndicator.center = imageView.center;
[imageView addSubview:activityIndicator];
[self.view bringSubviewToFront:activityIndicator];
[activityIndicator startAnimating];
[imageView sd_setImageWithURL:imgUrl completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[activityIndicator removeFromSuperview];
}];
}
}
});
});
[wrapperView addSubview:imageView];
[wrapperView addSubview:btnclk];
if ([[NSString stringWithFormat:@"%@",[[messageArray objectAtIndex:indexPath.row] objectForKey:@"fromid"]] isEqualToString:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:LOGGED_IN_USER]]]) {
if (imageView.frame.size.width > nameLabel.frame.size.width) {
wrapperViewX = self.view.frame.size.width - imageView.frame.size.width - 14.f;
} else {
wrapperViewX = self.view.frame.size.width - nameLabel.frame.size.width - 14.f;
}
} else {
wrapperViewX = 7.f;
}
if (imageView.frame.size.width > nameLabel.frame.size.width) {
wrapperViewWidth = 4.f + imageView.frame.size.width + 4.f;
} else {
wrapperViewWidth = 4.f + nameLabel.frame.size.width + 4.f;
}
wrapperViewHeight = imageView.frame.origin.y + imageView.frame.size.height + 4.f;
} else if ([messageType isEqualToString:MESSAGE_TYPE_STICKER]) {
CGRect imageRect = CGRectMake(4.f, (nameLabel.frame.origin.y + nameLabel.frame.size.height + 4.f), 100.f, 100.f);
if (messageString == nil) {
[imageView setImage:[UIImage imageWithData:[[messageArray objectAtIndex:indexPath.row] objectForKey:MESSAGE]]];
} else {
[imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"StickersFramework.bundle/%@",messageString]]];
}
[imageView setFrame:imageRect];
[wrapperView addSubview:imageView];
if ([[NSString stringWithFormat:@"%@",[[messageArray objectAtIndex:indexPath.row] objectForKey:@"fromid"]] isEqualToString:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:LOGGED_IN_USER]]]) {
if (imageView.frame.size.width > nameLabel.frame.size.width) {
wrapperViewX = self.view.frame.size.width - imageView.frame.size.width - 14.f;
} else {
wrapperViewX = self.view.frame.size.width - nameLabel.frame.size.width - 14.f;
}
} else {
wrapperViewX = 7.f;
}
if (imageView.frame.size.width > nameLabel.frame.size.width) {
wrapperViewWidth = 4.f + imageView.frame.size.width + 4.f;
} else {
wrapperViewWidth = 4.f + nameLabel.frame.size.width + 4.f;
}
wrapperViewHeight = imageView.frame.origin.y + imageView.frame.size.height + 4.f;
} else {
[textView setFont:[UIFont systemFontOfSize:14.0f]];
[textView setTextContainerInset:UIEdgeInsetsZero];
[textView setBackgroundColor:[UIColor clearColor]];
/* Disable scroll and editing */
[textView setEditable:NO];
[textView setScrollEnabled:NO];
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
[paragraph setLineSpacing:2.0f];
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont systemFontOfSize:14.f], NSFontAttributeName,paragraph ,NSParagraphStyleAttributeName,nil];
[messageString boundingRectWithSize:constraints options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributesDictionary context:nil];
[textView setText:messageString];
CGSize sizeMessage = [textView sizeThatFits:constraints];
textView.frame = CGRectMake(0.0f,(nameLabel.frame.origin.y + nameLabel.frame.size.height + 4.f), sizeMessage.width, sizeMessage.height);
[wrapperView addSubview:textView];
if ([[NSString stringWithFormat:@"%@",[[messageArray objectAtIndex:indexPath.row] objectForKey:@"fromid"]] isEqualToString:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:LOGGED_IN_USER]]]) {
if (textView.frame.size.width > nameLabel.frame.size.width) {
wrapperViewX = self.view.frame.size.width - textView.frame.size.width - 14.f;
} else {
wrapperViewX = self.view.frame.size.width - nameLabel.frame.size.width - 14.f;
}
} else {
wrapperViewX = 7.f;
}
if (textView.frame.size.width > nameLabel.frame.size.width) {
wrapperViewWidth = 4.f + textView.frame.size.width + 4.f;
} else {
wrapperViewWidth = 4.f + nameLabel.frame.size.width + 4.f;
}
wrapperViewHeight = textView.frame.origin.y + textView.frame.size.height + 4.f;
attributesDictionary = nil;
}
[wrapperView setFrame:CGRectMake(wrapperViewX, 7.f, wrapperViewWidth, wrapperViewHeight)];
/* Define timeString & timeLabel */
NSTimeInterval _interval;
NSDate *date;
if ([[NSString stringWithFormat:@"%@",[[messageArray objectAtIndex:indexPath.row] objectForKey:@"sent"]] length] > 10) {
_interval = ([[NSString stringWithFormat:@"%@",[[messageArray objectAtIndex:indexPath.row] objectForKey:@"sent"]] doubleValue]/1000);
date = [NSDate dateWithTimeIntervalSince1970:_interval];
} else {
_interval = ([[NSString stringWithFormat:@"%@000",[[messageArray objectAtIndex:indexPath.row] objectForKey:@"sent"]] doubleValue]/1000);
date = [NSDate dateWithTimeIntervalSince1970:_interval];
}
NSDateFormatter *_formatter = [[NSDateFormatter alloc] init];
[_formatter setLocale:[NSLocale currentLocale]];
_formatter.dateStyle = NSDateFormatterMediumStyle;
_formatter.timeStyle = NSDateFormatterShortStyle;
_formatter.doesRelativeDateFormatting = YES;
NSString *timeString = [_formatter stringFromDate:date];
[timeLabel setFont:[UIFont systemFontOfSize:10.f]];
[timeLabel setTextColor:[UIColor colorWithRed:103.0f/255.0f green:103.0f/255.0f blue:103.0f/255.0f alpha:1.0]];
[timeLabel setNumberOfLines:0];
[timeLabel setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary *attributesTime = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont systemFontOfSize:10.f], NSFontAttributeName, nil];
CGRect timeRect = [timeString boundingRectWithSize:constraints options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributesTime context:nil];
CGSize sizeTime = timeRect.size;
[timeLabel setText:timeString];
if ([[NSString stringWithFormat:@"%@",[[messageArray objectAtIndex:indexPath.row] objectForKey:@"fromid"]] isEqualToString:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:LOGGED_IN_USER]]]) {
[nameLabel setTextColor:[UIColor whiteColor]];
[textView setTextColor:[UIColor whiteColor]];
[wrapperView setBackgroundColor:[UIColor colorWithRed:49.0f/255.0f green:140.0f/255.0f blue:231.0f/255.0f alpha:1.0f]];
[wrapperView setBackgroundColor:[UIColor colorWithRed:49.0f/255.0f green:140.0f/255.0f blue:231.0f/255.0f alpha:1.0f]];
[timeLabel setFrame:CGRectMake(self.view.frame.size.width - sizeTime.width - 7.f, (wrapperView.frame.origin.y + wrapperView.frame.size.height + 2.f ), sizeTime.width, sizeTime.height)];
} else {
[wrapperView setBackgroundColor:[UIColor colorWithRed:235.0f/255.0f green:235.0f/255.0f blue:235.0f/255.0f alpha:1.f]];
[wrapperView setBackgroundColor:[UIColor colorWithRed:235.0f/255.0f green:235.0f/255.0f blue:235.0f/255.0f alpha:1.f]];
[timeLabel setFrame:CGRectMake(7.f, (wrapperView.frame.origin.y + wrapperView.frame.size.height + 2.f ), sizeTime.width, sizeTime.height)];
}
[chatViewCell.contentView addSubview:wrapperView];
[chatViewCell.contentView addSubview:timeLabel];
return chatViewCell;
}
在这里,我为此实现了聊天功能,我们必须更新屏幕上的聊天消息,为此我使用NsPostNotifictaionCenter。我还使用调度重新加载我的表,以避免无响应的 UI 问题。我在这里发布代码:
- (void)chatRoomMessageUpdateNotifier: (NSNotification *)notification {
dispatch_queue_t concurrentQueue1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(concurrentQueue1, ^{
if ([[[notification userInfo] objectForKey:@"TAG"] isEqualToString:@"0"]) {
messageArray = [NSMutableArray new];
[messageArray removeAllObjects];
messageArray = [[notification userInfo] objectForKey:@"data"];
if ([messageArray count] > 0) {
dispatch_async(dispatch_get_main_queue(), ^{
[chatTable reloadData];
});
}
}
});
}
我不知道我做错了什么。这里的主要问题是我的 UI 变得无响应。完全欢迎所有想法。