Question

I had an comments array, witch contains the comment message and the reply message and follow the reply message.want to show all the messages orderly, seems its An infinite loop.

My code is like below:

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
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"PId == %d",[[message objectForKey:@"Id"] intValue]];
    NSArray *tempArray = [self.replyCommentArray filteredArrayUsingPredicate:predicate];
    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;

        float lastHeight = 0.0;

        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.numberOfLines = 0;
            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];
            followButton.tag = indexPath.row + 10000;
            [followBackView addSubview:followButton];
            [followBackView bringSubviewToFront:followButton];
            //                            CGSize size = [followCommentLabel.text sizeWithFont:followCommentLabel.font
            //                                                              constrainedToSize:CGSizeMake(240, 800)
            //                                                                  lineBreakMode:NSLineBreakByWordWrapping];
            //                            followCommentLabel.frame = CGRectMake(5, 0, 418, size.height);

            predicate = [NSPredicate predicateWithFormat:@"PId == %d",[[replyMessage objectForKey:@"Id"] intValue]];
            NSArray *subTempArray = [self.replyCommentArray filteredArrayUsingPredicate:predicate];

            lastHeight += 21;
            if (subTempArray.count > 0) {
                lastHeight += subTempArray.count * 21;

                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);

                float followLabelHeight = 0.0;
                for (int j =0 ; j < subTempArray.count; j ++) {
                    NSDictionary *ohterMessage = [subTempArray objectAtIndex:j];
                    replyStr = [NSString stringWithFormat:@"%@reply%@: %@",[ohterMessage objectForKey:@"Username"],[replyMessage objectForKey:@"Username"],[ohterMessage objectForKey:@"Comment"]];
                    TTTAttributedLabel *subCommentLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(5, followCommentLabel.frame.origin.y + followCommentLabel.frame.size.height + followLabelHeight, followBackView.frame.size.width, 21)];
                    //                                subCommentLabel.numberOfLines = 0;
                    subCommentLabel.font = [UIFont systemFontOfSize:13];
                    subCommentLabel.textColor = [UIColor darkGrayColor];
                    subCommentLabel.backgroundColor = [UIColor clearColor];
                    [subCommentLabel setText:replyStr afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
                        NSRange redColorRange = [[mutableAttributedString string] rangeOfString:[NSString stringWithFormat:@"%@reply%@:",[ohterMessage objectForKey:@"Username"],[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:subCommentLabel];

                    UIButton *subfollowButton = [UIButton buttonWithType:UIButtonTypeCustom];
                    subfollowButton.frame = subCommentLabel.frame;

                    NSString *subIdentify = [NSString stringWithFormat:@"%d-%d",i,j];
                    [subfollowButton setTitle:subIdentify forState:UIControlStateSelected];
                    subfollowButton.titleLabel.textColor = [UIColor clearColor];

                    [subfollowButton addTarget:self action:@selector(handleFollowComment:) forControlEvents:UIControlEventTouchUpInside];
                    subfollowButton.tag = indexPath.row + 10000;
                    [followBackView addSubview:subfollowButton];
                    [followBackView bringSubviewToFront:subfollowButton];
                    //                            CGSize size = [followCommentLabel.text sizeWithFont:followCommentLabel.font
                    //                                                              constrainedToSize:CGSizeMake(240, 800)
                    //                                                                  lineBreakMode:NSLineBreakByWordWrapping];
                    //                            followCommentLabel.frame = CGRectMake(5, 0, 418, size.height);

                    predicate = [NSPredicate predicateWithFormat:@"PId == %d",[[ohterMessage objectForKey:@"Id"] intValue]];
                    NSArray *subFollowTempArray = [self.replyCommentArray filteredArrayUsingPredicate:predicate];
                    followLabelHeight += 21;
                    if (subFollowTempArray.count > 0) {
                        followLabelHeight += subFollowTempArray.count * 21;

                        lastHeight += subFollowTempArray.count * 21;

                        float height = followBackView.frame.size.height;
                        height += 21*subFollowTempArray.count;

                        followBackView.frame = CGRectMake(followBackView.frame.origin.x, followBackView.frame.origin.y, followBackView.frame.size.width, height);

                        for (int k =0 ; k < subFollowTempArray.count; k ++) {
                            NSDictionary *ohterFollowMessage = [subFollowTempArray objectAtIndex:k];
                            NSString *replyFollowStr = [NSString stringWithFormat:@"%@reply%@: %@",[ohterFollowMessage objectForKey:@"Username"],[ohterMessage objectForKey:@"Username"],[ohterFollowMessage objectForKey:@"Comment"]];
                            TTTAttributedLabel *subFollowCommentLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(5, subCommentLabel.frame.origin.y + subCommentLabel.frame.size.height + k * 21, followBackView.frame.size.width, 21)];
                            //                                subCommentLabel.numberOfLines = 0;
                            subFollowCommentLabel.font = [UIFont systemFontOfSize:13];
                            subFollowCommentLabel.textColor = [UIColor darkGrayColor];
                            subFollowCommentLabel.backgroundColor = [UIColor clearColor];
                            [subFollowCommentLabel setText:replyFollowStr afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
                                NSRange redColorRange = [[mutableAttributedString string] rangeOfString:[NSString stringWithFormat:@"%@reply%@:",[ohterFollowMessage objectForKey:@"Username"],[ohterMessage 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:subFollowCommentLabel];
                        }
                    }

                }

            }


        }
        [cell.contentView addSubview:followBackView];
    }
Was it helpful?

Solution

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

     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top