Autostart UITableViewCell

I have a problem with autorun. I have a UITableViewCell and a view inside. I want this view to be smaller than the cell, so I added the restrictions:

  • Vertical space (10) up
  • Vertical space (10) below
  • Horizontal space (10) leading space
  • Horizontal space (10) finite space

When the TableView is displayed, the cell is displayed well, but then if I scroll down the View table and scroll the backup, the view in the cell changes and now occupies the full space in the cell.

The correct cell: enter image description here

Invalid cell (after scrolling) enter image description here

I already searched for this, but did not find anything like it. I think this person had the same problem, but no answer: A similar problem

Thanks for any help in advance!

+4
source share
3 answers

I finally found the answer to my question here . In my regular UITableViewCell class, I did the following:

 - (void)awakeFromNib { [super awakeFromNib]; for (NSLayoutConstraint *cellConstraint in self.constraints) { [self removeConstraint:cellConstraint]; id firstItem = cellConstraint.firstItem == self ? self.contentView : cellConstraint.firstItem; id seccondItem = cellConstraint.secondItem == self ? self.contentView : cellConstraint.secondItem; NSLayoutConstraint* contentViewConstraint = [NSLayoutConstraint constraintWithItem:firstItem attribute:cellConstraint.firstAttribute relatedBy:cellConstraint.relation toItem:seccondItem attribute:cellConstraint.secondAttribute multiplier:cellConstraint.multiplier constant:cellConstraint.constant]; [self.contentView addConstraint:contentViewConstraint]; } } 

I assume that the presentation of the content changes its content or something like that. If there is an easier explanation for this and an easier way to do this, I am still waiting for other answers. Thank you for help!

+3
source

I had the same issue today using Xcode 5 and storyboards. Initially, my custom table view cells displayed correctly, but when I scroll through the table view and the cells were reused, the layout messed up. The fix was to call setNeedsLayout as follows:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { SettingTableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"Setting" forIndexPath:indexPath]; [tableViewCell.contentView setNeedsLayout]; return tableViewCell; } 

affairs>
REAL problem:

I named my label textLabel , which matches the name of the built-in label property of UITableViewCell. This caused all sorts of oddities. As soon as I changed the name of the outlet property to a unique name, my layout problems were resolved.

+1
source

This problem arises because Your UITableView Reuse Cell instantly creates a new one. I am giving you some suggestion that may help you.

1) add a UIView between

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { /// Your code; } return cell; } 

1) Add your UIView to cell.contentView.

Edition:

Before following my Edited answer, I want to tell you that the following code is bad for memory management, because it will create a new cell for each row of the UITableView , so be careful.

But it is better if the UITableView has a limited string (50-100 maybe) use the following code if it suits you.

 NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil) { } 
0
source

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


All Articles