简体   繁体   中英

TablewView Cell all cell in the same row

在此处输入图片说明

在此处输入图片说明

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;

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.

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.

尝试使用视觉约束代替setFrame可能会有帮助!

在此处输入图片说明 I 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

在此处输入图片说明

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"];
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM