TableView Cell 同一行的所有单元格
TablewView Cell all cell in the same row
本项目不涉及故事板
最有问题的部分我不知道在哪里解决这个问题。
我正在使用 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];
此函数在 NSNotification 上调用
CGRect moveToRect = CGRectMake(-(msgFrame.origin.x+ msgFrame.size.width), msgFrame.origin.y, msgFrame.size.width, msgFrame.size.height);
[_msgTableView setFrame:moveToRect];
这在扩展视图中被调用
[_msgTableView sizeWith:CGSizeMake(screenW, 250)];
[_msgTableView layoutAbove:_bottomView margin:kDefaultMargin];
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];
}
这是函数
处的 Cell For 行
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;
这是 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;
请确保单条消息由单个cell加载,且cell的高度正确。您似乎在同一个单元格中创建了很多 UILabel。
据我了解,当有新消息通知时,您没有根据数据保持单元格的高度。因此它们似乎是重叠的。
现在你必须保持新通知的高度,并且获取的数据应该添加到你的主数组(数据源)中。因此可以很容易地 coordinated.and 单元格不会重叠。
要保持单元格的高度,请使用 "heightForRowAtIndexPath "table 视图的委托函数并相应地保持特定单元格的高度。
还有一件事,请确保单个消息将在单个单元格中。这将相应地管理。
尝试使用 visual constraints 而不是 setFrame 可能会有帮助!
我找到了解决办法。
我创建了一个 nib 文件作为 tableview cell 的扩展,我注册为 nib
但我接受@mayanksengar 的回答,因为它给了我很好的洞察力
我刚刚创建了一个 xib 文件以及 .h 和 .m
将 xib 注册到 table 查看
[_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"];
}
本项目不涉及故事板
最有问题的部分我不知道在哪里解决这个问题。 我正在使用 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];
此函数在 NSNotification 上调用
CGRect moveToRect = CGRectMake(-(msgFrame.origin.x+ msgFrame.size.width), msgFrame.origin.y, msgFrame.size.width, msgFrame.size.height);
[_msgTableView setFrame:moveToRect];
这在扩展视图中被调用
[_msgTableView sizeWith:CGSizeMake(screenW, 250)];
[_msgTableView layoutAbove:_bottomView margin:kDefaultMargin];
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];
}
这是函数
处的 Cell For 行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;
这是 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;
请确保单条消息由单个cell加载,且cell的高度正确。您似乎在同一个单元格中创建了很多 UILabel。
据我了解,当有新消息通知时,您没有根据数据保持单元格的高度。因此它们似乎是重叠的。
现在你必须保持新通知的高度,并且获取的数据应该添加到你的主数组(数据源)中。因此可以很容易地 coordinated.and 单元格不会重叠。
要保持单元格的高度,请使用 "heightForRowAtIndexPath "table 视图的委托函数并相应地保持特定单元格的高度。
还有一件事,请确保单个消息将在单个单元格中。这将相应地管理。
尝试使用 visual constraints 而不是 setFrame 可能会有帮助!
但我接受@mayanksengar 的回答,因为它给了我很好的洞察力
我刚刚创建了一个 xib 文件以及 .h 和 .m
将 xib 注册到 table 查看
[_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"];
}