Is there anyway to change the height of a cell dynamically whilst scrolling a UITableView?
I need to change the height of a number of cells when the scroll position reaches a certain point as the user drags it down past the top. I can do this successfully by issuing a reloadData, however it's very abrupt since the cells just vanish.
I've also tried reloading the cells and running the begin/end updates on the table view, however in both cases the animation sends the scroll view back to the top.
I'd like to animate the cell height change without messing up the current content offset of the drag in progress.
Tim
Use the UITableViewDelegate and implement the method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
This solve your issue?
If it is not solving your issue, try to explain better your goal, with examples/images.
Related
I am working on a quite complicated case
I have a nested UICollectionView
The outer UICollectionView A is for vertical scrolling,
and it's the first cell, is UICollectionView B, for horizontal scrolling.
It has been implemented perfectly, but now there is one more requirement
When scrolling the first cell to the second page, there is one more banner appeared on the bottom of the first cell.
Therefore I need to update the height of cell when scrolling.
If I try to call invalid layout when scrolling, it will be very low efficient. Are there any more methods to support it?
Solved.
First after received the data from the server, each cell height will be calculated and [collectionview reload] will be called.
Then after layouting the collectionview, the size for each cell is cached according to the indexpath.
To implement the 'realtime' resizing, the gesture will call a resizing function directing through a delegate, and directly modify the size cache and call [collectionview.layout invalidlayout].
It looks smooth.
I am displaying feed items in a tableview. Depending on the item, the cell must be larger or smaller--for example larger to accomodate a photo or larger still if it references another feed item such as an article and I have to display the item referenced.
In most cases, I've used autolayout to get the cells to auto expand or contract based on the content. However, in a couple cases, this was proving next to impossible so I reset the value of a constraint in code. For example, if there is no photo in one case, I shrink the height of the cell in code as the cell was not shrinking on its own.
This works fine when the page loads. But when you scroll and reuse cells, it leads to weird effects such as the cell expanding vertically or lines being pushed into one another.
Some questions on SO suggest using the method:
-(void) tableView:(UITableView *)tableView didEndDisplayingCell:(IDFeedCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//Undo changes here
}
The problem is I don't know how to undo changes. For example, if I made a cell smaller by setting
self.height.constant = 100;
Should I set it back to the constant value in storyboard?
What value can I set it to when I don't know what the next item to resuse the cell will need in the way of dimensions.
Thanks for any suggestions. BTW, autolayout shows no issues.
I have got UiCollectionView with rounded cel (on iPad 3-4, on iPhone 1-2).
I need show view below any clicked cell as on screen (with change text length).
I spend a lot of time thinking about this.
My first step was make custom cell that show the frame below cells, but it was wrong step.
Rounded cells changed padding when this was added...
I am using RAReorderableLayout, additionally to swipe cells.
Any idea?
Please help me with this problem.
You could make it a custom UIView, which has 3 parts: the part before the tick, the image with the tick and the part after. Then in the delegate method for
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
you can call an update method on this view, that will change the widths of the three parts according to the selected index in the collection view.
You could even send the width of the collection view cell in this update method to use it for calculating the right widths.
Hope this helps! Good luck!
I'm building a chat using UITableView. Naturally, the row heights of each chat message cell will vary widely. I've correctly calculated the row height for each cell and the Table View performs as it should.
However, I want to implement estimatedHeightForRowAtIndexPath: to speed up performance in case of many messages. The problem with this is that it affects the scrolling behaviour of scrollToRowAtIndexPath:atScrollPosition:Animated:.
In my viewWillAppear life cycle method I tell the Table View to scroll down to the latest message (i.e. the bottom message), which performs well if I don't use estimatedHeightForRowAtIndexPath:, but as soon as I do it seems as if only the value returned from this method is used to calculate the scroll position and I end up in the wrong position.
Is it possible to benefit from the efficiency gains of using estimatedHeightForRowAtIndexPath: without it affecting scroll behaviour?
UITableView is a subclass of UIScrollView so the method
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
of UIScrollView may help you to achieve what you want
In my table view I am using custom image for the UITableView cell's background. It works perfectly except for the fact it hides the row separator. And now there is no row visible.
Is there a way to increase the distance so that cells will get separated.
Please help!!!
Thanks,
Without seeing what you are seeing, it's a bit hard to tell. That said, you could try adding the separator to the image so that it shows up in the cell.
If you want to change the height of the cell, implement the -tableView:heightForRowAtIndexPath: method in the table view delegate. Just return the correct height for the row at indexPath. I believe there is also a way to set the rowHeight in the table view properties in interface builder if all cells will be the same, but I tend to stay away from IB when possible.
As far as my knowledge you can add a line image to the cell as a footer.So that you can see the separator
I believe you have to override the -setFrame: method in your custom cell and return the same frame with height minus 1 pixel.
Also be sure to call [super layoutSubviews]; in your -layoutSubviews method if you have overridden this method with some custom implementation.
LineSeparator may be invisible to you because of your background image in your cell. Try to change the style of your separator.
Alternative Way:
Otherwise You have to add a one pixel line to the bottom of your Custom cell background image by your designer and if it's still not visible in your tableView, then you have to increase a height for your cell in heightForRowAtIndexPath: method.
Make sure your view if you have one that is inside your Cell is exactly the same size as your cell's size.
Make sure:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
is setting your cell heights correctly for all cells.