Uitableview Strange Layout Behavior Changes on Scroll

UITableView strange layout behavior changes on scroll

Getting auto layout to work on a UITableViewCell is to ensure you have constraints to pin each subview on all sides — that is, each subview should have leading, top, trailing and bottom constraints.

Furthermore, you need a clear line of constraints going from the top to the bottom of the contentView. This ensures that auto layout correctly determines the height of the contentView based on its subviews.

The tricky part is that Interface Builder often won’t warn you if you’re missing some of these constraints; auto layout simply doesn’t return the correct heights when you run the project. For example, it may return 0 for the cell height, which is a clue that your constraints need more work.

If you run into issues when working with your own projects, try adjusting your constraints until the above criteria are met.

check the link for details:http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift

TableView collapsing cell while scrolling to another: weird behavior

First let's define what we mean by "without scrolling" - we mean that the cells more of less stay the same. So we want to find a cell that we want to be the anchor cell. From before the changes to after the changes the distances from the cell's top to the top of the screen is the same.

var indexPathAnchorPoint: IndexPath?
var offsetAnchorPoint: CGFloat?

func findHighestCellThatStartsInFrame() -> UITableViewCell? {
return self.tableView.visibleCells.filter {
$0.frame.origin.y >= self.tableView.contentOffset.y
}.sorted {
$0.frame.origin.y > $1.frame.origin.y
}.first
}

func setAnchorPoint() {
self.indexPathAnchorPoint = nil;
self.offsetAnchorPoint = nil;

if let cell = self.findHighestCellThatStartsInFrame() {
self.offsetAnchorPoint = cell.frame.origin.y - self.tableView.contentOffset.y
self.indexPathAnchorPoint = self.tableView.indexPath(for: cell)
}
}

we call this before we start doing stuff.

 func bubbleTappedHandler(sender: UITapGestureRecognizer) {
self.setAnchorPoint()
....

Next we need to set the content offset after we are doing doing our changes so that the cell moves back to where it is suppose to.

func scrollToAnchorPoint() {
if let indexPath = indexPathAnchorPoint, let offset = offsetAnchorPoint {
let rect = self.tableView.rectForRow(at: indexPath)
let contentOffset = rect.origin.y - offset
self.tableView.setContentOffset(CGPoint.init(x: 0, y: contentOffset), animated: false)
}
}

Next we call it after we are done doing our changes.

  self.tableView.beginUpdates()
self.tableView.endUpdates()
self.tableView.layoutSubviews()
self.scrollToAnchorPoint()

The animation can be a little strange because there is a lot going on at the same time. We are changing the content offset and the sizes of the cell at the same time, but if you place your finger next to the first cell that top is visible you will see it ends up in the correct place.

IOS: Custom UITableViewCell - Strange Behaviour after Scrolling

When you scroll a tableview, the cells are reused (dequeueReusableCellWithIdentifier) to optimize performance. In the code above, a lineSeparator image view is added to the cell each time the cellForRowAtIndexPath method is invoked. If the cell is used 5 times, it will have 5 image views added.

One way address this is to remove the lineSeparator image view from the cell before it is reused. This is typically done in the cell's prepareForReuse method.

In the cellForRowAtIndexPath, add a tag to the lineSeparator image view (e.g., lineSeparator.tag = 100;

In your cell's class, implement the prepareForReuse method. E.g.:

-(void)prepareForReuse{
UIView *lineSeparatorView = [self.contentView viewWithTag:100];
[lineSeparatorView removeFromSuperview];
}

dequeued UITableViewCell has incorrect layout until scroll (using autolayout)

If you name a property on a UITableViewCell subclass textLabel or defaultTextLabel, then IB will ignore the constraints you have specified and override them with default ones, with no warnings issued.

This is the case even on cells designed in IB with the Custom style, which have no visible textLabel or detailTextLabel properties.

This also happen if add a property of type UIImageView property on a UITableViewCell subclass and name it imageView.

UITableView dynamic cell heights only correct after some scrolling

I don't know this is clearly documented or not, but adding [cell layoutIfNeeded] before returning cell solves your problem.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TestCell"];
NSUInteger n1 = firstLabelWordCount[indexPath.row];
NSUInteger n2 = secondLabelWordCount[indexPath.row];
[cell setNumberOfWordsForFirstLabel:n1 secondLabel:n2];

[cell layoutIfNeeded]; // <- added

return cell;
}

UITableView scrolling odd behaviour

I finally found a solution that worked!

In the end I had to put [self.tableView reloadData]; into the viewDidAppear method.

That solved the problem.

Strange behaviour of vertical uiscrollbar at uitableview

The issue was that the code i mentioned was called even when the user was scrolling the uitableview. It has nothing to do with the speed of the scroll. I just added a value in the scrollviewdidbegindragging and set it to TRUE and then when the scrollviewdidenddragging occurs i set it to FALSE. I have put this code in to my function

If (!scrollingvalue)
{
//execute code
}

When the value is FALSE (which means that the uitableview is not dragged by the user) then the code is executed else it is not executed. Hope it helps someone.

UITableViewController weird behavior after popping a view controller

I managed to fix the first issue. It seems like the tableview is not taking into account the 44 points of the UIToolbar.

Save the tableview offset in prepareForSegue: (save it in a CGPoint property)

self.tableViewScrollOffset = self.tableView.contentOffset;

Then, in viewWillAppear:, check if it has been modified. If so, restore it.

if(self.tableView.contentOffset.y != self.tableViewScrollOffset.y) {
[self.tableView setContentOffset:self.tableViewScrollOffset];
self.tableViewScrollOffset = CGPointZero;
}


Related Topics



Leave a reply



Submit