Try this, this may not be the exact solution. But you can follow this approach with some minor changes
- (NSArray *) replyforPid:(int) pID {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"PId == %d",pID];
NSArray *tempArray = [self.replyCommentArray filteredArrayUsingPredicate:predicate];
return tempArray;
}
- (void) addReplyInView:(UIView *) followBackView forPid:(int) pID lastHeight:(float) lastHeight {
NSArray *tempArray = [self replyforPid:pID];
if ([tempArray count] > 0) {
if (lastHeight != 0) {
float height = followBackView.frame.size.height;
height += 21*subTempArray.count;
followBackView.frame = CGRectMake(followBackView.frame.origin.x, followBackView.frame.origin.y, followBackView.frame.size.width, height);
}
for (int i = 0; i < tempArray.count; i ++) {
NSDictionary *replyMessage = [tempArray objectAtIndex:i];
NSString *replyStr;
replyStr = [NSString stringWithFormat:@"%@: %@",[replyMessage objectForKey:@"Username"],[replyMessage objectForKey:@"Comment"]];
TTTAttributedLabel *followCommentLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(5, lastHeight + 5, followBackView.frame.size.width, 21)];
followCommentLabel.font = [UIFont systemFontOfSize:13];
followCommentLabel.textColor = [UIColor darkGrayColor];
followCommentLabel.backgroundColor = [UIColor clearColor];
[followCommentLabel setText:replyStr afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
NSRange redColorRange = [[mutableAttributedString string] rangeOfString:[NSString stringWithFormat:@"%@:",[replyMessage objectForKey:@"Username"]] options:NSCaseInsensitiveSearch];
// Core Text APIs use C functions without a direct bridge to UIFont. See Apple's "Core Text Programming Guide" to learn how to configure string attributes.
NSDictionary *colorDic = [NSDictionary dictionaryWithObjectsAndKeys:UIColorFromRGB(0xbf6327),kCTForegroundColorAttributeName, nil];
[mutableAttributedString addAttributes:colorDic range:redColorRange];
return mutableAttributedString;
}];
[followBackView addSubview:followCommentLabel];
UIButton *followButton = [UIButton buttonWithType:UIButtonTypeCustom];
followButton.frame = followCommentLabel.frame;
NSString *identify = [NSString stringWithFormat:@"%d",i];
[followButton setTitle:identify forState:UIControlStateSelected];
followButton.titleLabel.textColor = [UIColor clearColor];
[followButton addTarget:self action:@selector(handleFollowComment:) forControlEvents:UIControlEventTouchUpInside];
[followBackView addSubview:followButton];
[followBackView bringSubviewToFront:followButton];
lastHeight += 21;
[self addReplyInView:followBackView forPid:[[replyMessage objectForKey:@"Id"] intValue] lastHeight:lastHeight];
}
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.commentArray.count > 0) {
//comments
NSDictionary *message = [self.commentArray objectAtIndex:indexPath.row];
if ([FHWServiceManager isValidObject:[message objectForKey:@"UserIcon"]]) {
[cell.photoImageView setImageWithURL:[NSURL URLWithString:[message objectForKey:@"UserIcon"]] placeholderImage:[UIImage imageNamed:@"face_poor"]];
}else {
cell.photoImageView.image = [UIImage imageNamed:@"face_poor"];
}
cell.photoImageView.layer.cornerRadius = 15;
cell.photoImageView.layer.masksToBounds = YES;
cell.nameLabel.text = [message objectForKey:@"Username"];
cell.nameLabel.textColor = UIColorFromRGB(0xbf6327);
cell.contentLabel.text = [message objectForKey:@"Comment"];
cell.timeLabel.text = [DateUtil timePassedDescriptionFor:[DateUtil dateFromRFC3339DateString:[message objectForKey:@"CreateTime"]]];
//follow comment
NSArray *tempArray = [self replyforPid:[[message objectForKey:@"Id"] intValue]];
if (tempArray.count > 0) {
UIImageView *triangleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(67, 80, 10, 5)];
triangleImageView.image = [UIImage imageNamed:@"comment_reply_arrow_top"];
[cell.contentView addSubview:triangleImageView];
UIView *followBackView = [[UIView alloc] initWithFrame:CGRectMake(55, 85, 245, tempArray.count * 21 + 10)];
followBackView.backgroundColor = UIColorFromRGB(0xf6f6f6);
followBackView.layer.cornerRadius = 5;
[self addReplyInView:followBackView forPid:[[message objectForKey:@"Id"] intValue] intValue] lastHeight:0.0];
}
}
}