When I set rowHeight to a UITableView, shouldn't the cell height also be changed?

Question:
When I set rowHeight to a UITableView , shouldn't the cell height also be changed?
Here is a situation that makes me think about it:


I want to set a separate row for each cell of the table view below, what else, I want to set the row height for it from 44 to 32 . The result I want is as follows:
EpioB.png

Setting the line height is done correctly:

 - (void)viewDidLoad { [super viewDidLoad]; [self.tableView setRowHeight:32.0f]; [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; //... } // Even use the delegate of UITableView - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 32.0f; } 

However, when I added a separate row for the cell, I ran into a problem: I want to set a separate row at the bottom of the cell, so I set the position of y on cell.frame.size.height - 1.0f . Unfortunately, the result is shown below:
kPqPv.png

When I made the selection, the cell changed as shown below:

1. Selected line No. 1:

UuEft.png

2. Selected line No. 2:

H5FCm.png

3. Selected row No. 3:

TZn5i.png

It seems that the cell height before it was selected was 44 , when it is selected, it has changed to 32 . They were overlapped one by one, like cards, right? Weird!

Main code:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; [cell.textLabel setFont:[UIFont fontWithName:@"Futura-Medium" size:15.0f]]; UIView * seperateLine = [[UIView alloc] initWithFrame:CGRectMake(10.0f, cell.frame.size.height - 1.0f, 300.0f, 1.0f)]; NSLog(@">>>>>>>>>>>>>>>> %f", cell.frame.size.height); [seperateLine setBackgroundColor:[UIColor grayColor]]; [cell.contentView addSubview:seperateLine]; [seperateLine release]; } //... } 

I tried checking cell.frame.size.height , and finally it was 44 . And then I will replace the line

 UIView * seperateLine = [[UIView alloc] initWithFrame:CGRectMake(10.0f, cell.frame.size.height - 1.0f, 300.0f, 1.0f)]; 

to

 UIView * seperateLine = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 31.0f, 300.0f, 1.0f)]; 

It worked as the first image shown above. But the cell height was still 44 , they (which we just don't see completely) still overlap. What I did was just add a separate line , where y position 32 , but its total height is 44 .


So what do you think of this ?:

+4
source share
2 answers

Problem:

According to Apple HIG, the default area is 44, so by default all controls have 44 heights.

you define a new UITableViewCell inside

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

which by default is 44 and you do not change anything in the UITableViewCell frame and return it.

Decision:

try setting the frame size to UITableViewCell ie cell.frame = CGRectMake(0.0f, 0.0f, 320.0f, 32.0f)

+3
source

Please review the documentation and find the tableView:heightForRowAtIndexPath: delegate tableView:heightForRowAtIndexPath: With which you can set the height for each row. It can be fixed or can be dynamic, as well as at your request.

Hope this helps.

+2
source

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


All Articles