时间好少,看点东西记录一下,以免以后忘记了!好好学习,天天向上!
1.首先在vc中的必须是注册cell
[self.footerTable registerClass:[KuShowCommentTableViewCell class] forCellReuseIdentifier:@"commentCell"];
2.在 cellForRowAtIndexPath方法中实现 KuShowCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"commentCell"]; [self configureCell:cell atIndexPath:indexPath]; 3. [self configureCell:cell atIndexPath:indexPath];
###注意这个frame的设置yes layout设置成no cell.fd_enforceFrameLayout = YES; // Enable to use "-sizeThatFits:"
- (void)configureCell:(KuShowCommentTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
cell.fd_enforceFrameLayout = YES; // Enable to use "-sizeThatFits:" NSString *str; NSString *text; if ([[self.arr[indexPath.row] parentId] length]) { str = @"回复了"; text = [NSString stringWithFormat:@"%@%@%@ \n%@",[self.arr[indexPath.row] nickName],str,[self.arr[indexPath.row] nickName2],[self.arr[indexPath.row] content]]; }else { str = @"评论了"; if ([self.kuxXiuModel.nickName isKindOfClass:[NSNull class]] || self.kuxXiuModel.nickName == nil) { text = [NSString stringWithFormat:@"%@%@%@ \n%@",[self.arr[indexPath.row] nickName],str,self.name,[self.arr[indexPath.row] content]]; }else { text = [NSString stringWithFormat:@"%@%@%@ \n%@",[self.arr[indexPath.row] nickName],str,self.kuxXiuModel.nickName,[self.arr[indexPath.row] content]]; } } NSString *text1 = [EmojiTool emojiStringFromString:text]; NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:text1]; NSRange range1, range2; range1 = NSMakeRange(0, [[self.arr[indexPath.row] nickName] length]);//通过NSRange来划分片段 if (![[self.arr[indexPath.row] parentId] length]) { if ([self.kuxXiuModel.nickName isKindOfClass:[NSNull class]] || self.kuxXiuModel.nickName == nil) { range2 = NSMakeRange([[self.arr[indexPath.row] nickName] length]+3, [self.name length]); }else { range2 = NSMakeRange([[self.arr[indexPath.row] nickName] length]+3, [self.kuxXiuModel.nickName length]); } }else{ range2 = NSMakeRange([[self.arr[indexPath.row] nickName] length]+3, [[self.arr[indexPath.row] nickName2] length]); } UIColor *color1 = [UIColor colorWithRed:0.54 green:0.76 blue:0.94 alpha:1.00]; [attributedStr addAttribute:NSForegroundColorAttributeName value:color1 range:range1];//给不同的片段设置不同的颜色 [attributedStr addAttribute:NSForegroundColorAttributeName value:color1 range:range2]; cell.attributedString = attributedStr;
-
重写attributedString的set方法
- (void)setAttributedString:(NSMutableAttributedString *)attributedString{
self.firstL.attributedText = attributedString; }
//如果是frame的,要在自定义cell里面重写这个方法 设置cell的高度 - (CGSize)sizeThatFits:(CGSize)size { CGFloat totalHeight = 0; NSLog(@"%f",size.height); totalHeight += [self.firstL sizeThatFits:size].height; totalHeight+= 10; // 边距 return CGSizeMake(size.width, totalHeight);
}