TablewView Cell同一行中的所有单元格

时间:2017-12-26 08:34:09

标签: ios objective-c tableview xcode9.2

enter image description here

enter image description here

此项目不涉及故事板

最有问题的部分我无法弄清楚在哪里解决这个问题。 我正在使用SDK。

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

在NSNotification上调用此函数

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

在扩展视图中调用

    [_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];
}

当通知消息通知

时调用此方法
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;

这是函数

中的单元格行
        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;

这是ConfigWith函数

sc.textFile("path/to/dir/containing/the/files").count

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

为了保持单元格的高度,使用表格视图的“heightForRowAtIndexPath”委托函数并相应地保持特定单元格的高度。

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

答案 2 :(得分:0)

尝试使用visual constraints代替setFrame可能有所帮助!

答案 3 :(得分:0)

enter image description here我找到了解决方案。 我创建一个nib文件作为tableview单元格的扩展名,我注册为nib

但我接受了@mayanksengar给出了很好的见解答案

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

enter image description here

将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"];
    }
相关问题