Custom UITableViewCell with auto layout and accessory

I have a custom table view cell that uses automatic layout and has a disclosure indicator as an additional view. The cell size of the cells on the screen is completely erroneous when first displayed:

As you can see, the cell occupies about 1.5 screens of space:

enter image description here

However, if I rotate the device and rotate it backwards, it looks fine:

enter image description here

As you can see here, I did not do anything complicated:

enter image description here

I have a very small solution to make:

-(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self.tableView reloadData]; } 

But this clearly causes a β€œflash” when you first see the screen. In a more complex scenario, the flash is much more obvious.

I have another workaround, but this throws an auto-layout exception:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { BasicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasicCell" forIndexPath:indexPath]; cell.basicLabel.text = @"Hello this is just some text that should get the label to go over multiple lines"; [cell.basicLabel layoutIfNeeded]; return cell; } 

An exception:

enter image description here

At least this method does not let me blink the UI.

If I remove the look of the accessory, it really works great.

UPDATE: I added a sample project to github: https://github.com/fwaddle/TableCellAccessoryTest

UPDATE # 2: It turns out another work around this error is to layout the cell in code. I just tried to do the same in the code, and it did not throw a warning and worked fine. Looks like an IB error.

Any ideas how to get around this? Thanks.

+6
source share
2 answers

Therefore, even creating restrictions in the code sometimes does not fix this problem. You seem to need a few more changes. In your custom table cell, add the following, especially if you change the type of accessory depending on the contents of the cell (e.g. Checkmark):

 -(void) setAccessoryType:(UITableViewCellAccessoryType)accessoryType { [super setAccessoryType:accessoryType]; [self setNeedsUpdateConstraints]; } 

Also remove the prototype cell in the storyboard and register your class instead:

 -(void) viewDidLoad { [super viewDidLoad]; [self.tableView registerClass:[MyCustomCell class] forCellReuseIdentifier:@"MyCustomCell"]; } 

I sometimes find that I still need to force labels (especially multi-line labels) on re layout in cellForRowAtIndexPath:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCustomCell" forIndexPath:indexPath]; cell.customLabel.text = ..... [cell.customLabel layoutIfNeeded]; return cell; } 

All of the above problems have been fixed, so I hope that they will be useful to others, and you do not spend much time on this.

I still haven’t heard anything from Apple about the bug report, but I assume this is pretty normal.

0
source

Implement the following delegate method as this solved the problem for me.

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath 

The view of the table sends this message to its delegate shortly before it uses the cell to draw a row, thereby allowing the delegate to configure before displaying it. This method gives the delegate the opportunity to override the state properties that were previously set by the view table, for example, selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when the lines are animated when they enter or exit.

Add this code to the TableViewController table:

  - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ BasicCell *basicCell = (BasicCell *)cell; basicCell.basicLabel.text = @"Hello this is just some text that should get the label to go over multiple lines"; } 
+3
source

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


All Articles