只有在滚动tableview之后才能显示tableview单元格

时间:2017-03-27 07:13:00

标签: ios objective-c tableview tableviewcell autoresize

我尝试所有答案来正确设置tableview单元格,但没有任何答案适合我,当我滚动tableview只有滚动tableview动态单元格显示正确后,我必须显示多个类型数据,以便我现在使用多个部分第一部分我必须显示一个问题详细数据,以便我做以下代码。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {

        if (indexPath.section == 0) {
            CGFloat estimatedHeight = 150;
            static NSString *cellIdentifier = @"QuestionDetailCell";

            quesDetailCell = (QuestionDetailCell *)[tableView dequeueReusableCellWithIdentifier:
                                                    cellIdentifier];
            if (quesDetailCell == nil) {
                quesDetailCell = [[QuestionDetailCell alloc]initWithStyle:
                                  UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            }

            /*Question Title*/
            quesDetailCell.lblQuestionTitle.text = [dictionary valueForKey:@"title"];
            CGFloat HeightForQuestionTitle  = [self getLabelHeight:quesDetailCell.lblQuestionTitle];

            /*Question*/
            NSRange r;
            NSString *s = [NSString stringWithFormat:@"%@",[dictionary valueForKey:@"question"]];
            while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
                s = [s stringByReplacingCharactersInRange:r withString:@""];
            quesDetailCell.questionlbl.text =s;
            CGFloat HeightForQuestion = [self getLabelHeight:quesDetailCell.questionlbl];

            /*By User Name*/
            NSString *namestr=[NSString stringWithFormat:@"By %@ %@",[dictionary objectForKey:@"firstname"],[dictionary objectForKey:@"lastname"]];
            quesDetailCell.namelbl.text =namestr;
            CGFloat HeightForNamelbl = [self getLabelHeight:quesDetailCell.namelbl];

            /*Category*/
            quesDetailCell.engineeringlbl.text =[NSString stringWithFormat:@"%@",[dictionary objectForKey:@"category_name"]];
            CGFloat HeightForCategory = [self getLabelHeight:quesDetailCell.engineeringlbl];

            CGFloat maxHeight = HeightForNamelbl > HeightForCategory ? HeightForNamelbl : HeightForCategory;
            CGFloat lblTimeHeight = quesDetailCell.timelbl.frame.size.height;
            CGFloat totalHeightOfCell = 8 + HeightForQuestionTitle  + 8 + HeightForQuestion + 8 + maxHeight  + 8 +  lblTimeHeight + quesDetailCell.vwSubButtons.frame.size.height;

            if (estimatedHeight < totalHeightOfCell) {
                return totalHeightOfCell;
            }else{
                return estimatedHeight;
            }

        }else if (indexPath.section == 1){
            return 0;
        }
        else{
            return 105;
        }
    }else{
        return 154;
    }
}

对于显示单元格我正在这样做,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"QuestionDetailCell";
 if (indexPath.section==0) {
        quesDetailCell = (QuestionDetailCell *)[tableView dequeueReusableCellWithIdentifier:
                                        cellIdentifier];
        if (quesDetailCell == nil) {
            quesDetailCell = [[QuestionDetailCell alloc]initWithStyle:
                    UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }

        /*set User Profile*/
        quesDetailCell.profileimg.layer.cornerRadius = quesDetailCell.profileimg.frame.size.height /3;
        quesDetailCell.profileimg.layer.masksToBounds = YES;
        quesDetailCell.profileimg.layer.borderWidth = 0;
        NSString *imagestr=[dictionary objectForKey:@"profilepic"];
        [quesDetailCell.profileimg sd_setImageWithURL:[NSURL URLWithString:imagestr]
                           placeholderImage:[UIImage imageNamed:@"ic_my_colonities_details_2_profile.png"]];
        quesDetailCell.btnQuestionDetailProfile.tag = indexPath.row;
        [quesDetailCell.btnQuestionDetailProfile addTarget:self action:@selector(mainprofilebtnpress:) forControlEvents:UIControlEventTouchUpInside];

        /*calculate height of  Question Title*/

        quesDetailCell.lblQuestionTitle.text = [dictionary valueForKey:@"title"];
        CGFloat HeightForQuestionTitle  = [self getLabelHeight:quesDetailCell.lblQuestionTitle];
        [quesDetailCell.lblQuestionTitle sizeToFit];
        quesDetailCell.lblQuestionTitle.numberOfLines = 0;


        /*set Question Title*/

        CGFloat questionTitleXPoint = quesDetailCell.profileimg.frame.size.width + 8;

        quesDetailCell.lblQuestionTitle.frame = CGRectMake(quesDetailCell.lblQuestionTitle.frame.origin.x, quesDetailCell.lblQuestionTitle.frame.origin.y, quesDetailCell.lblQuestionTitle.frame.size.width, HeightForQuestionTitle);

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

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

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

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

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

        /*Question*/
        NSRange r;
        NSString *s = [NSString stringWithFormat:@"%@",[dictionary valueForKey:@"question"]];
        while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
            s = [s stringByReplacingCharactersInRange:r withString:@""];
        quesDetailCell.questionlbl.text =s;
        CGFloat HeightForQuestion = [self getLabelHeight:quesDetailCell.questionlbl];
        [quesDetailCell.questionlbl sizeToFit];
        quesDetailCell.questionlbl.numberOfLines = 0;

        /*questionTitleXPoint = question label x possition*/
        quesDetailCell.imgQuestionIcon.frame = CGRectMake(questionTitleXPoint, quesDetailCell.imgQuestionIcon.frame.origin.y, 12, 12);

        quesDetailCell.questionlbl.frame = CGRectMake(quesDetailCell.questionlbl.frame.origin.x, quesDetailCell.lblQuestionTitle.frame.origin.y + HeightForQuestionTitle + 4, quesDetailCell.questionlbl.frame.size.width, HeightForQuestion);

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

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

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

        /*By User Name*/
        NSString *namestr=[NSString stringWithFormat:@"By %@ %@",[dictionary objectForKey:@"firstname"],[dictionary objectForKey:@"lastname"]];
        quesDetailCell.namelbl.text =namestr;
        CGFloat HeightForNamelbl = [self getLabelHeight:quesDetailCell.namelbl];
        [quesDetailCell.namelbl sizeToFit];
        quesDetailCell.namelbl.numberOfLines = 0;

        /*Category*/
        quesDetailCell.engineeringlbl.text =[NSString stringWithFormat:@"%@",[dictionary objectForKey:@"category_name"]];
        CGFloat HeightForCategory = [self getLabelHeight:quesDetailCell.engineeringlbl];
        [quesDetailCell.engineeringlbl sizeToFit];
        quesDetailCell.engineeringlbl.numberOfLines = 0;

        quesDetailCell.namelbl.frame = CGRectMake(quesDetailCell.namelbl.frame.origin.x, quesDetailCell.questionlbl.frame.origin.y + HeightForQuestion + 4 , quesDetailCell.namelbl.frame.size.width, HeightForNamelbl);

        quesDetailCell.engineeringlbl.frame =
        CGRectMake(quesDetailCell.engineeringlbl.frame.origin.x,
                   quesDetailCell.questionlbl.frame.origin.y + HeightForQuestion + 4,
                   quesDetailCell.engineeringlbl.frame.size.width,
                   HeightForCategory);

        CGFloat attachmentlblXPoint = quesDetailCell.engineeringlbl.frame.origin.x - 8 - quesDetailCell.attachmentlbl.frame.size.width;

        CGFloat attachmentklblYPoint = quesDetailCell.engineeringlbl.frame.origin.y;

        quesDetailCell.attachmentlbl.frame = CGRectMake(attachmentlblXPoint, attachmentklblYPoint - 4 , quesDetailCell.attachmentlbl.frame.size.width, quesDetailCell.attachmentlbl.frame.size.height);

         CGFloat btnAttachmentXPoint = attachmentlblXPoint - 4 - quesDetailCell.btnAttachment.frame.size.width ;

        quesDetailCell.btnAttachment.frame = CGRectMake(btnAttachmentXPoint, attachmentklblYPoint, quesDetailCell.btnAttachment.frame.size.width, quesDetailCell.btnAttachment.frame.size.height);

        quesDetailCell.timelbl.frame = CGRectMake(quesDetailCell.timelbl.frame.origin.x, quesDetailCell.engineeringlbl.frame.origin.y + HeightForCategory + 4, quesDetailCell.timelbl.frame.size.width, quesDetailCell.timelbl.frame.size.height);

        CGFloat timeIconXPoint = quesDetailCell.timelbl.frame.origin.x - 4 - quesDetailCell.imgQuestionTimeIcon.frame.size.width;

        quesDetailCell.imgQuestionTimeIcon.frame = CGRectMake(timeIconXPoint, quesDetailCell.timelbl.frame.origin.y, quesDetailCell.imgQuestionTimeIcon.frame.size.width,quesDetailCell.imgQuestionTimeIcon.frame.size.height);

        /*set VWSubQuestion*/

        quesDetailCell.vwSubButtons.frame = CGRectMake(quesDetailCell.vwSubButtons.frame.origin.x, quesDetailCell.imgQuestionTimeIcon.frame.origin.y + quesDetailCell.imgQuestionTimeIcon.frame.size.height + 8 , quesDetailCell.vwSubButtons.frame.size.width, quesDetailCell.vwSubButtons.frame.size.height);

        return quesDetailCell;

    }

初始外观如下When First load tableview it look like this

向下滚动然后再向上滚动: After Scroll tableview cell look like this

请帮帮我。

0 个答案:

没有答案