In the project, there is a page for the activity details page, in which the related content of the activity is placed in a view with a white background color, and the other background color is gray. The finished image is shown below

Because the content is divided into activity time, amount, rules and so on and separated by a horizontal line, so I decided to use tableView to draw, create a cell file to add content, and then create a controller to create tableView load.

Content is placed in each cell according to the classification. However, the length of the text varies and is bound to change in subsequent use. The height of the cell ADAPTS to the content

/ / row high adaptive _tableView rowHeight = UITableViewAutomaticDimension; / / give a forecast, when calculate out the default. Use this highly _tableView estimatedRowHeight = 100;Copy the code

Can be set.

And the height of the TableView is going to be calculated according to the height of each cell and how do you do that?

Think of it this way: calculate the height of each cell and pass their height values to the Controller for summation.

Select ReactiveObjc (RAC) to transfer values:

Declare subject in cell.h:

@property (nonatomic, strong) RACSubject *subject;
Copy the code

Cell.m lazy load creation:

- (RACSubject *)subject{
    if(! _subject) { _subject = [RACSubject subject]; }return _subject;
}
Copy the code

Statement in the agent that creates the cell

ABLaborActivityDetailViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
Copy the code

The setLayout method in cell.m is used to create the control. The setLayout method in cell.m is used to create the control. , the setIndex method will be used to assign value to the control content.

So to confirm when to start passing values, we declare a global variable

BOOL _isLayout; // Status switchCopy the code

Set the initial state to NO in the layout method – (void)setLayout

_isLayout = NO;
Copy the code

In the setIndex assignment method:

- (void)setIndex:(NSInteger)index{
    self.titleLabel.text = self.dataArray[index][@"title"];
    self.detailLabel.text = self.dataArray[index][@"detail"]; // When assigning cell contents via index and array, set the switch to YES. The number of passes is determined by the array we created. Each pass goes to layoutIfNeeded. _isLayout = YES; // Call layoutSubviews [self layoutIfNeeded]; }Copy the code

After setLayout creates an empty control, let _isLayout = YES;

This method is added at the end of the setLayout method:

- (void)layoutSubviews{ [super layoutSubviews]; Self.size. Height = self.size. Height = self.size.if(_isLayout == YES) { [self.subject sendNext:@(self.size.height)]; // Print cell high check NSLog(@"High CELL = % f",self.size.height);
    }
    
Copy the code

The first 10 lines are the initial height before the empty control is assigned (44) -> the height after the content is obtained. The loop is repeated five times (because there are five cells). The last 5 lines are the final height of each cell.


function: - [ABLaborActivityDetailViewCell layoutSubviews] line: 82 content: high CELL = 44.000000function: - [ABLaborActivityDetailViewCell layoutSubviews] line: 82 content: high CELL = 50.000000function: - [ABLaborActivityDetailViewCell layoutSubviews] line: 82 content: high CELL = 44.000000function: - [ABLaborActivityDetailViewCell layoutSubviews] line: 82 content: high CELL = 50.000000function: - [ABLaborActivityDetailViewCell layoutSubviews] line: 82 content: high CELL = 44.000000function: - [ABLaborActivityDetailViewCell layoutSubviews] line: 82 content: high CELL = 50.000000function: - [ABLaborActivityDetailViewCell layoutSubviews] line: 82 content: high CELL = 44.000000function: - [ABLaborActivityDetailViewCell layoutSubviews] line: 82 content: high CELL = 81.000000function: - [ABLaborActivityDetailViewCell layoutSubviews] line: 82 content: high CELL = 44.000000function: - [ABLaborActivityDetailViewCell layoutSubviews] line: 82 content: high CELL = 96.500000function: - [ABLaborActivityDetailViewCell layoutSubviews] line: 82 content: high CELL = 50.000000function: - [ABLaborActivityDetailViewCell layoutSubviews] line: 82 content: high CELL = 50.000000function: - [ABLaborActivityDetailViewCell layoutSubviews] line: 82 content: high CELL = 50.000000function: - [ABLaborActivityDetailViewCell layoutSubviews] line: 82 content: high CELL = 81.000000function: - [ABLaborActivityDetailViewCell layoutSubviews] line: 82 content: high CELL = 96.500000Copy the code

This completes the transfer of values in the cell and completes the code in the cell

To the corresponding controller:

We need to create a global variable to receive incoming cell high:

{ CGFloat _tableViewHeight; // Height of the receiving cell BOOL _isEnd; // Refresh switch}Copy the code

There’s a BOOL variable here, because there’s going to be an infinite loop when you refresh the TableView when you receive the value, and to get rid of the infinite loop, you need to give a switch to tell when the refresh stops.

Initialize two global variables in viewDidLoad:

_tableViewHeight = 0; _isEnd = NO; // Give the initial stateCopy the code

In the cell content broker:

weakifyySelf; [cell.subject subscribeNext:^(NSNumber *x) {stronggSelf; // Receive cell values and sum _tableViewHeight = _tableViewHeight + [xfloatValue]; // On line 5, when the switch is in NO state, execute the switch to YES to prevent the refresh; Tableview range, and refresh the data, make it display the correct range, height calculation needs to adjust according to the actual situation, here +50 to display completelyif(indexPath.row == 4 && _isEnd == NO) { _isEnd = YES; self.tableView.size = CGSizeMake(SCREEN_WIDTH - 30, _tableViewHeight+50); [self.tableView reloadData]; }}];Copy the code

Here is the final effect again:

Some details:

Activity details, need to give a maximum width, consider adaptation, should calculate, rather than to determine the value, here is an example (left such as “challenge time” these title I gave the control width, calculation to subtract) :

/ / word wrap _detailLabel. NumberOfLines = 0; / /, 31, 33 for left and right margins, 67 is on the left side of the title length _detailLabel. PreferredMaxLayoutWidth = SCREEN_WIDTH - 31-33-67;Copy the code

The authors introduce

  • Li Hong: iOS development engineer of Guangzhou Reed APP Team

Push the information

  • We are recruiting partners, interested partners can send your resume to [email protected], note: from the nuggets community
  • For details, please click here –> Guangzhou Reed Information Technology