iOS-将NSString放入UITableViewCell

iOS-将NSString放入UITableViewCell,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在尝试将twitter用户名和tweet放入一个单元格中,并通过以下方式实现: NSString *username = [NSString stringWithFormat:@"%@ @%@", [tweet[@"user"] objectForKey:@"name"], [tweet[@"user"] objectForKey:@"screen_name"]]; NSString *tweet = [NSString stringWithFormat:@"%@", tweet[@"t

我正在尝试将twitter用户名和tweet放入一个单元格中,并通过以下方式实现:

NSString *username = [NSString stringWithFormat:@"%@ @%@", [tweet[@"user"] objectForKey:@"name"], [tweet[@"user"] objectForKey:@"screen_name"]];


NSString *tweet = [NSString stringWithFormat:@"%@", tweet[@"text"]];

我在
heightforrowtaindexpath
中尝试了以下方法,如此处的另一个问题所示:

NSString *header = [NSString stringWithFormat:@"%@ @%@", [tweet[@"user"] objectForKey:@"name"], [tweet[@"user"] objectForKey:@"screen_name"]];


NSString *tweetData = [NSString stringWithFormat:@"%@", tweet[@"text"]];

CGRect head = [header boundingRectWithSize:CGSizeMake(300.f, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:nil context:nil];

CGRect tweetData2 = [tweetData boundingRectWithSize:CGSize(300.f, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:nil context:nil];

CGSize size = head.size;
CGSize size2 = tweetData2.size;

CGFloat n = size.height;
CGFloat m = size2.height;

NSLog(@"%f", n + m);

return (n + m) > 65.0 ? (m + n) : 65.0;

我只是想显示整个tweet,并相应地调整单元格大小,而不是让“…”出现

 NSString *text =  [NSString stringWithFormat:@"%@ @%@", [tweet[@"user"] objectForKey:@"name"], [tweet[@"user"] objectForKey:@"screen_name"]];

    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIFont fontWithName:@"System" size:30], NSFontAttributeName,
                                          nil];

    CGRect framee = [text boundingRectWithSize:CGSizeMake(Maximum_width,Maximum_height)
                                       options:NSStringDrawingUsesLineFragmentOrigin
                                    attributes:attributesDictionary
                                       context:nil];

    CGSize size = framee.size;
    NSLog(@"indexPAth %d %f",indexPath.section, size.height);
    return size.height +40;
试试这个

定义注释标签宽度170 定义注释标签最小高度60 定义注释\u标签\u填充10 设置selectedIndex=-1;在视图中没有加载

-(CGFloat)getLabelHeightForIndex:(NSInteger)index
{
    CGSize maximumSize = CGSizeMake(COMMENT_LABEL_WIDTH, 200);
    
    CGSize labelHeighSize = [yourstring sizeWithFont: [UIFont systemFontOfSize:12.0]
                                                                              constrainedToSize:maximumSize
                                                                                  lineBreakMode:NSLineBreakByWordWrapping];
    return labelHeighSize.height;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
            if(selectedIndex == indexPath.row)
            {
                if([self getLabelHeightForIndex:indexPath.row]>yourminimumcellheight)
                {
                    return [self getLabelHeightForIndex:indexPath.row]+40;
                }
                else
                {
                    return COMMENT_LABEL_MIN_HEIGHT;
                }
            }
            else {
                return COMMENT_LABEL_MIN_HEIGHT;
            }
}



  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:         (NSIndexPath *)indexPath
    {
        static NSString *simpleTableIdentifier = @"Cell";
        
         UITableViewCell *cell;
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault      reuseIdentifier:simpleTableIdentifier];
            if(selectedIndex == indexPath.row)
            {
                CGFloat labelHeight = [self getLabelHeightForIndex:indexPath.row];
                UILabel *comment;
                if(labelHeight >your minimum cell height)
                {
                 comment = [[UILabel alloc] initWithFrame:CGRectMake(x,y,width,labelHeight)];
                            
                 }
                 else
                 {
                  comment = [[UILabel alloc] initWithFrame:CGRectMake(x,y,width,height)];
                            
                 }
        }
       else
       {
        comment = [[UILabel alloc] initWithFrame:CGRectMake(x,y,width,height)];
       }
    
   }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(selectedIndex == indexPath.row)
           {
               selectedIndex = -1;
               [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
               
               return;
           }
           
           if(selectedIndex >= 0)
           {
               NSIndexPath *previousPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
               selectedIndex = indexPath.row;
               [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade];
           }
           
           selectedIndex = indexPath.row;
           [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

这是您就同一基本问题提出的第三个问题。为什么?@rmaddy我想了一点,但无法得出最终结果,希望有人能引导我走向正确的方向。这并不能解释为什么你在短时间内发布了3个问题。请停下来。坚持一个问题。如果您需要澄清,您可以编辑您的问题。没必要在几分钟后再发布另一个问题。真不敢相信你在3小时内发布了3次同样的问题。我记得当你第一次问这个问题时,我看到了一个很好的回答…thanx,如果你还有其他问题,请提问。