0

enter image description here

enter image description here

No storyboard involve in this project

The most problematic part i cant figure out where to solve this. Im Using 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];

this function are called on NSNotification

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

this is called in extended view

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

this is called when new message notify by 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];
}

this is the Cell For row at function

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;

this is the ConfigWith function

        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;
halfer
  • 19,824
  • 17
  • 99
  • 186
Muhammad Asyraf
  • 1,748
  • 15
  • 20

4 Answers4

0

Please make sure that a single message was loaded by a single cell,and the height of cell is correct. It looks like you created lots of UILabels in the same cell.

Tao.Yu
  • 1
  • 1
  • the first image shows that the cell are correctly setup as 1 UiLabel per cell. but the positioning mess up... it keep pusing the cell to top for some reason – Muhammad Asyraf Dec 26 '17 at 10:16
0

As per my understanding when a new message notification has come, you are not maintaining the height of the cells according to the data. Hence they seem to be overlapped.

Now you have to maintain the height of the new notification, And the data is obtained should be added to your main array(data source). And hence can be easily coordinated.and cells are not get overlapped.

For maintaining the height of the cells, use "heightForRowAtIndexPath "delegate functions of the table view and Maintain the height of a particular cell accordingly.

And one more thing, please be sure that single message will be in the single cell. This will manage accordingly.

halfer
  • 19,824
  • 17
  • 99
  • 186
MayankSengar
  • 284
  • 5
  • 19
0

Try using visual constraints instead of setFrame may help!

0

enter image description hereI found the solution. i create a nib file as an extension to tableview cell and i register as nib

but im accepting @mayanksengar answer for giving me good insight

I just created a xib file and .h and .m

enter image description here

register xib to table view

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

finally used it

 NSString *cellIdentifier = @"customCell2";
    customCell2 *cell= [_msgTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell){
        [_msgTableView registerNib:[UINib nibWithNibName:@"customCell2" bundle:nil] forCellReuseIdentifier:@"customCell2"];
    }
Muhammad Asyraf
  • 1,748
  • 15
  • 20