2017-12-26 7 views
0

enter image description hereTablewView 셀

enter image description here

없음 스토리 보드는이 프로젝트

내가이 문제를 해결하기 위해 위치를 파악하지 못할 가장 문제가 일부 포함 동일한 행의 모든 ​​세포. 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]; 

이 함수는 통지

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

생하여 통지 할 때 새로운 메시지

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

불려 이것은 확장 뷰라고 NSNotification

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

에라고 들이이 ConfigWith 기능을 것입니다 기능

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; 

답변

0

새 메시지 알림이 왔을 때 내 이해에 따라 데이터에 따라 셀 높이를 유지하지 못합니다. 그러므로 그들은 겹쳐지는 것처럼 보입니다.

이제 새 알림의 높이를 유지해야하며 데이터는 주 어레이 (데이터 소스)에 추가되어야합니다. 따라서 쉽게 조정할 수 있으며 세포가 겹쳐지지 않습니다.

셀의 높이를 유지하려면 테이블 뷰의 "heightForRowAtIndexPath"대리자 기능을 사용하고 그에 따라 특정 셀의 높이를 유지하십시오.

그리고 한 가지 더 말씀 드리면 단일 메시지가 단일 셀에 포함되어야합니다. 이에 따라 적절하게 관리됩니다.

0

하나의 메시지가 하나의 세포에 의해로드하고, 셀의 높이가 정확하고 있는지 확인하십시오. 동일한 셀에 많은 UILabels을 만든 것처럼 보입니다.

+0

사용. 하지만 위치가 엉망이됩니다 ... 어떤 이유로 든 셀을 계속해서 퍼지게합니다. –

0

enter image description here 해결책을 찾았습니다. 는 내가있는 tableview 세포의 확장으로 nib 파일을 작성하고 메신저 나에게 난 그냥 XIB 파일과 .H 및 생성

좋은 통찰력을주는 @mayanksengar 대답을 받아들이는 펜촉

로하지만 난 등록합니다.테이블 뷰

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

행 m

enter image description here

레지스터 XIB 마침내 제 화상은 셀 셀당 1 UILabel의로 정확하게 설정 것을 보여준다 그것을

NSString *cellIdentifier = @"customCell2"; 
    customCell2 *cell= [_msgTableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (!cell){ 
     [_msgTableView registerNib:[UINib nibWithNibName:@"customCell2" bundle:nil] forCellReuseIdentifier:@"customCell2"]; 
    }