UIViews and variable height labels

Good, so at the moment I'm not in a table cell. (I saw a lot of threads relative to cell height.)

I have a detailed view with a title and description at the top of the view, followed by the table below. If the description will vary in length, how do I get the tableView to adjust it accordingly so that it always starts right below it?

I see that the apple is doing this in the app store app. If you look at the description of the application (as if you bought it), they have a description of the application, and wherever the scrollable screenshot panel ends. How do they perform different heights above a text description?

Do they do all this programmatically or can I use the layout controls in IB to do the same?

+3
source share
3 answers

you need to add a programmatic table view to your view and set its frame according to your view size

1> get the current detailView frame 2> add it in height and add a table view to your view

like

UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(initX , initY + DetailViewFrame.size.height, TableWidth, TableHeight) style:UITableViewStylePlain];
[view addSubView:table];
+1
source

​​IB , . ( ) - , . , , ( y ) , , . , -viewDidLoad -viewWillAppear:, , .

+1

, "sizeWithFont" , . :

- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath
{
  CGSize labelSize = CGSizeMake(200.0, 20.0);
  if ([string length] > 0)
    labelSize = [string sizeWithFont: [UIFont boldSystemFontOfSize: 17.0] constrainedToSize: CGSizeMake(labelSize.width, 1000) lineBreakMode: UILineBreakModeWordWrap];
  return 24.0 + labelSize.height;
}

"cellForRowAtIndexPath"

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.autoresizingMask = UIViewAutoresizingFlexibleHeight;       
    }

cell.textLabel.text = @"Your string with variable length";              
        [cell.textLabel setFont:[UIFont boldSystemFontOfSize:12]];
        //[cell.textLabel setAdjustsFontSizeToFitWidth:YES]; 
        [cell.textLabel setBaselineAdjustment:UIBaselineAdjustmentAlignBaselines]; 
        [cell.textLabel setLineBreakMode:UILineBreakModeWordWrap];
        [cell.textLabel setNumberOfLines:0];
return cell;
}

, ...

0

Source: https://habr.com/ru/post/1750469/


All Articles