I have a custom UITableViewCell with objects in it (such as UILabel, UITextView, UITextField etc.). When a button gets selected, a cell gets added to the tableView.
When I run it on the simulator, and the cell gets added, all the visible cell's and subviews height get really compact. (I do have auto constraint applied.)
....
[[self myTableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
If I do the following, the cells get back to normal:
NSArray* visibleCellIndex = self.myTableView.indexPathsForVisibleItems;
[self.myTableView reloadRowsAtIndexPaths:visibleCellIndex withRowAnimation:UITableViewRowAnimationFade];
[self.myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
The problem with reloading the visible cells, is: First, that's a workaround, not getting to the source of the problem. Second, it's not a fully functioning workaround, because the whole tableView scrolls all the way up for a second, then scroll back to position.
The reason why it was shrinking, is because, you have to implement the method of heightForRowAtIndexPath.
The only problem now, is that the tableView jumps up, then scrolls to position. Don't know why.
Does your target run only on iOS 8 and later? If yes, you can set self.tableView.rowHeight = UITableViewAutomaticDimension to enable Autolayout for your cells. Then, you also don't need to implement delegate tableView:heightForRowAtIndexPath:.
If you're already doing this, your problem probably lies in your custom cell. Maybe its constraints are not well defined? How do you initialize the cell's constraints?
Another idea is to trigger the layout pass manually in tableView:cellForRowAtIndexPath:. After the cell has been initialized and its text label values have been set, call:
[cell setNeedsLayout];
[cell layoutIfNeeded];
Related
After fetch data,i reload tableview. I use automatic dimension
don't override heightForRowAtIndexPath like below
self.tableView.estimatedRowHeight = 200;
self.tableView.rowHeight = UITableViewAutomaticDimension;
Everything is ok in iOS9. But in iOS8 appear like following link until scroll.
iOS8 appear
I tried:
[_tableView setNeedsLayout]; [_tableView layoutIfNeeded]; [_tableView reloadData]; After that occur else ui issue in iPhone 6Plus.
I set preferred Width automatic and not marker explicit.
I set uitableviewcell accessory type none.
Tried reload section.
Tried after did display cell, reload table view. After that while scrolling another view controller in page controller, occur same issue.
Tried [cell layoutIfNeeded]
Tried setPreferredMaxLayoutWidth
Try to reload data in viewDidappear or viewWillappear and check your constraints that it is perfect or not because UITableViewAutomaticDimension strongly recommended autolayout.
Update :
According to comments try to add this two line in cellforrowAtindexpath
[cell layoutIfNeeded];
[cell updateConstraintsIfNeeded];
In order to make UITableViewAutomaticDimension work you have to set all left, right, bottom, and top constraints relative to cell container view. If you missing like this then check that
hope this
will help :)
I'm using a UISegmentedControl which changes the contents of a UITableViewCell. After the segment changes I need to reload the cell to update the height of it. I do this by calling:
[self.tableView reloadRowsAtIndexPaths:#[self.segmentIndexPath] withRowAnimation:UITableViewRowAnimationNone];
However, this makes the UITableView scroll to the top. What is the best way to do this without having the table scroll?
Try to use [self.tableView visibleCells] and then find this cell and change
I have a tableviewcell, that on tap, grows in size, height wise, by updating the frame.
The problem is, the cells below don't adjust, move down to make it visible underneath, and the select row events are still based on the old sizes, before tap. I am using Facebook POP - which is handling animation, so tableview.beginUpdates() is out of the question, maybe?
You cannot manually update the frame of a UITableViewCell by changing its frame or bounds. Instead, you need to change the value returned by -tableView:heightForRowAtIndexPath: for that indexPath and then perform:
[self.tableView beginUpdates];
[self.tableView endUpdates];
This will cause the tableView to recalculate the heights of all the rows.
First, you need to make sure that you return the new height in heightForRowAtIndexPath, then you need to make the tableview update the cell. If you don't care about animation just call [tableview reloadData]. If you want animation you need to call [tableview reloadRowAtIndexPath: indexpath_of_your_cell]
I successfully implemented the self-resizing cells with autolayout. I added a UITextField to my content view that takes a string from the model, and the cell is now resizing correctly according to the length of the string.
The user is supposed to be able to edit this text field - how do I update the frame of the cell on user input (as the text field grows)?
I could resize the cells and the rest of the table view manually, but I figured there might be a better and simpler way to invalidate and refresh the frame of the cell that is being edited?
I want the frame changes to animate smoothly (e.g. as the textfield text requires a new line, this cell grows in height, and the cells below are pushed down accordingly).
If you just want to refresh a single cell you can do the following:
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForItem:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
Obviously you should change the indexpath to the correct one for your cell.
If you do this in something like textViewDidBeginEditing after checking if the contentSize of the UITextView has changed you should get the effect you're after.
It has been suggested that one scroll to the desired row in viewWillAppear, but this does not work with iOS 7. I have only been able to make this work in iOS 7 in the viewDidAppear callback. Unfortunately, you see the desired row scroll into view. I don't want to see any scrolling, I simply want the row to be visible when loaded. Can anyone suggest the proper way to do this in iOS 7?
It probably did not work in viewWillAppear, because that table had no data at this point.
Add [tableView reloadData];and it should work.
Let me get this straight: you want your table view to show a certain row at the top when the view apperas? Yes?
If so, you want:
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
with your cell indexPath, UITableViewScrollPositionTop as scrollPosition and animated NO like so
[tableView scrollToRowAtIndexPath:myExampleindexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
If you know the cell index then it's as simple as:
[tableView setContentOffset:CGPointMake(cellLocation.x,cellLocation.y) animated:NO];
Call that just after you load your tableView data and it will scroll to your cell being on top. There are other options as well:
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:currentRow inSection:currentSection] animated:NO scrollPosition:UITableViewScrollPositionTop];
Use this code with whatever scrollPosition you would like and Apple takes care of the bounding to the table (whereas setting the scrolling position is all user defined, it could be out of the table's view).
EDIT:
You could surround your selecting code with a call to UIView setting no animations allowed. That has worked for me in the past with different things, but I have never tried it in viewDidLoad.
[UIView setAnimationsEnabled:NO];
//Scroll the tableview
[UIView setAnimationsEnabled:YES];