简体   繁体   English

调整UITableView的大小

[英]Resizing UITableView

I'm working on the project. 我正在做这个项目。 Currently I'm facing a problem, I have 3 UITableView's in one ViewController. 目前,我面临一个问题,我在一个ViewController中有3个UITableView。 It looks something like this: 看起来像这样:

在此处输入图片说明

Now, The first TableView(top one), has just one row, that has a size of 55 pixels and it don;t have to be resizable. 现在,第一个TableView(顶部的一个)只有一行,大小为55像素,不需要调整大小。 Next, second one is the TableView, again with one row, but this row must to resize depends on the content of it. 接下来,第二个是TableView,再次具有一行,但是必须根据其内容调整该行的大小。 Right now this TableView displaying this row: 现在,此TableView显示此行:

在此处输入图片说明

This row must to resize depends on how long the text in it. 该行必须调整大小取决于其文本长度。 What I did to calculate the size is: 我所做的计算大小是:

if(tableView == self.usersPostOnTheWallTableView) {
    NSDictionary *propertyItems = [self.repliesItems objectAtIndex:indexPath.row];

    NSString *text = [self.postItems objectForKey:@"text"];

    NSLog(@"%@", text);

    CGSize constraintSize = CGSizeMake(280, MAXFLOAT);

    CGSize sizeText = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    self.height = MAX(sizeText.height, 44.0f);

    self.photosArray = [propertyItems objectForKey:@"attach_photos"];
    self.videosArray = [propertyItems objectForKey:@"attach_videos"];

    if((self.photosArray.count == 0) && (self.videosArray.count == 0)) {

        height += (CELL_CONTENT_MARGIN * 2);

    } else if ((self.videosArray.count > 0) && (self.photosArray.count == 0)) {

        height += 90 + (CELL_CONTENT_MARGIN * 2);

    } else if((self.photosArray.count > 0) && (self.videosArray.count == 0)) {

        height += 85 + (CELL_CONTENT_MARGIN * 2);
    }

}

Now, if the text is quite small, it fits the row well, and the row is resizes. 现在,如果文本很小,则很适合该行,并调整了行的大小。 Such as this: 如:

在此处输入图片说明

But, after the text comes bigger, this what happening: 但是,在文本变大之后,会发生以下情况:

在此处输入图片说明

在此处输入图片说明

Obviously I don't need the scrolling right there, and I would disable it. 显然,我不需要在那里的滚动,因此可以禁用它。 But the thing here is that it takes the size of the text, and it resize the label, you can see how many space it make from the top and from the bottom of the label. 但是这里的事情是,它占用了文本的大小,并调整了标签的大小,您可以看到它从标签的顶部和底部开始有多少空间。 But it don't put actual text there. 但这并没有在其中放置实际文本。 Another thing is that it didn't resize the TableView it self, when I'm trying to resize it, I get the overlapping of the bottom table by the middle table. 另一件事是它没有自行调整TableView的大小,当我尝试调整它的大小时,我得到了底表与中间表的重叠。

Can anyone recommend any solution of this, or maybe another way of implementation? 谁能推荐对此的任何解决方案,或者另一种实现方式?

Thanks in advance! 提前致谢!

U can do this is use tableviewDelegate method for setting cell height according your text like this for example: 您可以使用tableviewDelegate方法根据您的文本设置像元高度,例如:

This method will be called for every cell to decide how much cell should have height 将为每个单元格调用此方法,以决定应有多少个单元格的高度

  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    NSString *strDetails = [yourTextArray objectAtIndex:indexPath.row];
   //if(indexPath.row == 1)
   //{
     CGSize stringSize = [strDetails sizeWithFont:[UIFont fontWithName:@"Marker Felt" size:13] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:UILineBreakModeWordWrap]; // Provide text as well as font name in which your text will be displayed and constrainedToSize which defines width and height it should be maximum
     return stringSize.height+65; //Here 65 can be any value depending on Calculation  ImageView height + uppermost space + bottom space added
   //}
   //else 
   //{
   //  return 80 // anything u wishes
   //}
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM