2017-12-26 44 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]; 
} 

THIを通知するときに呼び出され

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

で呼び出されますsが、これは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; 

答えて

0

新しいメッセージ通知が来たときの私の理解によれば、データに応じてセルの高さを維持しているわけではありません。したがって、それらは重なり合っているようです。

これで、新しい通知の高さを維持する必要があります。データはメインアレイ(データソース)に追加する必要があります。したがって、容易に調整することができ、細胞は重複しない。

セルの高さを維持するには、テーブルビューの "heightForRowAtIndexPath"デリゲート関数を使用し、それに応じて特定のセルの高さを維持します。

さらに1つのメッセージが単一のセルに表示されるようにしてください。それに応じてこれが管理されます。

0

ある機能で、行のために細胞

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; 

ある単一のメッセージが単一のセルによってロードされたことを確認して、セルの高さは正しいですしてください。同じセルにたくさんのUILabelを作成したようです。

+0

を用います。 しかし、位置の混乱...それは何らかの理由でトップに細胞をpusingし続ける –

0

enter image description here解決策が見つかりました。 iは、テーブルビューのセルの拡張機能として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"]; 
    }