0

在此处输入图像描述

在此处输入图像描述

本项目不涉及情节提要

最有问题的部分我不知道在哪里解决这个问题。我正在使用 SDK。

_msgTableView = [[UITableView alloc] init];
_msgTableView.backgroundColor = [UIColor blackColor];
_msgTableView.delegate = self;
_msgTableView.dataSource = self;
_msgTableView.separatorInset = UIEdgeInsetsZero;
_msgTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_msgTableView.showsVerticalScrollIndicator = NO;
[self.view addSubview:_msgTableView];

此函数在 NSNotification 上调用

    CGRect moveToRect = CGRectMake(-(msgFrame.origin.x+ msgFrame.size.width), msgFrame.origin.y, msgFrame.size.width, msgFrame.size.height);
    [_msgTableView setFrame:moveToRect];

这在扩展视图中调用

[_msgTableView sizeWith:CGSizeMake(screenW, 250)];
[_msgTableView layoutAbove:_bottomView margin:kDefaultMargin];

当新消息通过 Notification 通知时调用

    [_msgDatas addObject:msg];
if (_msgDatas.count >= 500)
{

    NSRange range = NSMakeRange(_msgDatas.count - 100, 100);//只保留最新的100条消息
    NSArray *temp = [_msgDatas subarrayWithRange:range];
    [_msgDatas removeAllObjects];
    [_msgDatas addObjectsFromArray:temp];
    [_msgTableView reloadData];
}
else
{
    [_msgTableView beginUpdates];
    NSIndexPath *index = [NSIndexPath indexPathForRow:_msgDatas.count - 1 inSection:0];
    [_msgTableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationAutomatic];
    [_msgTableView endUpdates];
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_msgDatas.count-1  inSection:0];
if (indexPath.row < [_msgTableView numberOfRowsInSection:0])
{
    [_msgTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

这是函数中的 Cell For 行

MsgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LiveTextMessageCell"];
    if (cell == nil)
    {
        cell = [[MsgTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LiveTextMessageCell"];
    }
    id msg = _msgDatas[indexPath.row];
    if ([msg isKindOfClass:[ILVLiveTextMessage class]])
    {
        ILVLiveTextMessage *textMsg = (ILVLiveTextMessage *)msg;

        [cell configMsg:textMsg.sendId ? textMsg.sendId : [[ILiveLoginManager getInstance] getLoginId] msg:textMsg.text];
    }
    if ([msg isKindOfClass:[ILVLiveCustomMessage class]])
    {
        ILVLiveCustomMessage *customMsg = (ILVLiveCustomMessage *)msg;
        [cell configTips:customMsg.sendId];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

这是 ConfigWith 函数

        CGFloat selfW = [UIScreen mainScreen].bounds.size.width ;
        CGFloat selfH = 30;
        UIFont *msgFont = [UIFont fontWithName:@"Superclarendon" size:12];//Helvetica-Bold
        _nickname = profile.nickname;
        NSString *showInfo = [NSString stringWithFormat:@"%@: %@",profile.nickname, text];
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:showInfo];
        [attrStr addAttribute:NSForegroundColorAttributeName value:kColorGreen range:NSMakeRange(0, profile.nickname.length+1)];//+1是因为有个冒号
        [attrStr addAttribute:NSForegroundColorAttributeName value:kColorWhite range:NSMakeRange(profile.nickname.length+1, text.length+1)];//+1是因为有个空格
        [attrStr addAttribute:NSFontAttributeName value:msgFont range:NSMakeRange(0, showInfo.length)];//加粗
        _msgLabel.attributedText = attrStr;
        CGSize labelsize = [self textHeightSize:showInfo maxSize:CGSizeMake(selfW - kDefaultMargin*2, selfH * 3) textFont:msgFont];
        [_msgLabel setFrame:CGRectMake(_msgLabel.frame.origin.x, _msgLabel.frame.origin.y, labelsize.width + kDefaultMargin, labelsize.height)];
        [self setFrame:CGRectMake(0, 0, selfW, labelsize.height)];
        _height = labelsize.height;
        _msgLabel.hidden = NO;
        _tipsLabel.hidden = YES;
4

4 回答 4

0

请确保单个单元格加载了单个消息,并且单元格的高度正确。看起来您在同一个单元格中创建了很多 UILabel。

于 2017-12-26T09:54:01.993 回答
0

尝试使用视觉约束而不是 setFrame 可能会有所帮助!

于 2017-12-26T12:25:19.903 回答
0

根据我的理解,当收到新消息通知时,您没有根据数据维护单元格的高度。因此,它们似乎是重叠的。

现在您必须保持新通知的高度,并且获得的数据应该添加到您的主数组(数据源)中。因此可以很容易地协调。细胞不会重叠。

要保持单元格的高度,请使用“heightForRowAtIndexPath”委托表格视图的功能并相应地保持特定单元格的高度。

还有一件事,请确保单个消息将在单个单元格中。这将相应地进行管理。

于 2017-12-26T11:27:17.053 回答
0

在此处输入图像描述我找到了解决方案。我创建了一个 nib 文件作为 tableview 单元格的扩展名并注册为 nib

但我接受了@mayanksengar 的回答,因为它给了我很好的洞察力

我刚刚创建了一个 xib 文件和 .h 和 .m

在此处输入图像描述

将 xib 注册到表视图

[_msgTableView registerNib:[UINib nibWithNibName:@"customCell2" bundle:nil] forCellReuseIdentifier:@"customCell2"];

终于用上了

 NSString *cellIdentifier = @"customCell2";
    customCell2 *cell= [_msgTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell){
        [_msgTableView registerNib:[UINib nibWithNibName:@"customCell2" bundle:nil] forCellReuseIdentifier:@"customCell2"];
    }
于 2017-12-26T13:11:04.830 回答