Changing a specific uitableviewcell row height while scrolling a uitableview - uitableview

I have a UITableview with 2 sections, 1 row in the 1st section, and a variable amount of rows for the second section. When the user scrolls the table up (finger moving upwards), then the tableview does it's default thing, but when the user is at the top of the uitableview and scrolls down (finger moving downwards) then it should look like the first cell in the first section height increases as much as the user scrolls down (and releasing the touch will change the height of the row back to it's original height of 100). I tried
-(void)scrollViewDidScroll:(UIScrollView*)scrollView{
if(scrollView.contentOffset.y < 0){
[mainTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection: 0]] withRowAnimation:UITableViewRowAnimationNone];
}
}
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
if(indexPath.section == 0 && tableView.contentOffset.y < 0){
return -tableView.contentOffset.y+100;//Default height is 100
}
}
but the reloadRows method resets the contentOffset to 0 so the tableview just stutters when i pull down. It seems like if this doesn't work, then i'll have to write everything on a UIScrollView and it seems like a huge pain without recyclable cells.
Does anybody have any suggestions?

No need to reload the row, i just changed the frame directly. Make sure you set the cell.layer.masksToBounds = YES and make the subViews in the cell to be larger than the original cell height.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (mainTable.contentOffset.y < 0) {
UITableViewCell *cell = [mainTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
float rawr = mainTable.contentOffset.y;
cell.frame = CGRectMake(cell.frame.origin.x, rawr, cell.frame.size.width, 100-rawr);
}
}

Related

UICollectionView scrollToItemAtIndexPath scrolls to the wrong path

I have two UICollectionViews and when the user scrolls on one, the other should also change by scrolling to the same index path. I've implemented the following code so that, when the first UICollectionView decelerates and lands on one cell, the second UICollectionView should automatically scroll to that same IndexPath:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// Only do this for the first UICollectionView
if (scrollView.tag == 0) {
// Find the new IndexPath
CGRect visibleRect = (CGRect){.origin = self.filterCollectionView.contentOffset, .size = self.filterCollectionView.bounds.size};
CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
NSIndexPath *visibleIndexPath = [self.filterCollectionView indexPathForItemAtPoint:visiblePoint];
// On main thread, scroll second CollectionView to visibleIndexPath
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"Row: %li, Sec: %li", visibleIndexPath.row, visibleIndexPath.section);
[self.filterTitleCollectionView reloadData];
[self.filterTitleCollectionView layoutIfNeeded];
[self.filterTitleCollectionView setNeedsDisplay];
//scroll titles to same index path
[self.filterTitleCollectionView scrollToItemAtIndexPath:visibleIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
NSLog(#"Row: %li, Sec: %li", self.filterTitleCollectionView.indexPathsForSelectedItems[0].row, self.filterTitleCollectionView.indexPathsForSelectedItems[0].section);
});
}
}
For some reason, the filterTitleCollectionView does not scroll to the visibleIndexPath. Or if it does scroll, it scrolls to the wrong cell (in a predictable pattern). You can see the video of it here:
https://streamable.com/q2lnnw
The row number is in the title of the cell. As you can see, the second UICollectionView keeps scrolling to row 2 for some reason.
The correct IndexPath is printed, but it scrolls to the wrong IndexPath or doesn't scroll at all.

Calculate height of tableview cell according to height of tableview height inside cell?

I have a table view.I has one custom cell in it.In that custom cell i have a UITableView.This child tableview can have cells & their content height can be dynamic.So i want calculate height of each cell & multiply the no of cells.After that height which is calculated give height to the main cell.
I wil use below logic
**Total height of main cell = height of each cell in child tableview * no of cells + header**
but i don't know how to do it.Please guide me how can i do this
Try this
[tableView reloadData] ;
CGSize tableViewSize= tableView.contentSize; // here you get your tablview's total height
Get parent cell for your tableView
UITableViewCell *parentCellcell =(UITableViewCell *)tableView.superview.superview; // track your cell
if (parentCellcell){
NSIndexPath *indexPath = [tblViewParent indexPathForCell:cell];
[tblViewParent reloadRowsAtIndexPaths:[NSArray
arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
}

Changing cell height scrolls it off screen

I'm trying to expand a cell, by implementing different heights in heightForRow method. Everything works mostly ok, but when I'm expanding cell it somehow scrolls off screen. Here is what I mean (note feedback section):
As you can see there is feedback cell, and when I tap on smileys I want it to expand. I change the state and reload the row. This is what happens:
As you can see, the section completely scrolled off the screen and it is not visible.
If you scroll down, it is fine right there:
Here is the code that I use:
Method that triggers cell expansion:
self.feedbackCellStatus = FeedbackCellStatusExpanded;
NSIndexPath *indexPathToUpdate = [NSIndexPath indexPathForRow:0 inSection:[self indexOfSection:ReportSectionFeedback]];
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:#[indexPathToUpdate] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
Here is my height for row:
if (indexPath.section == [self indexOfSection:ReportSectionFeedback]) {
if (self.feedbackCellStatus == FeedbackCellStatusStarting) {
return FeedbackHeightStarting;
} else if (self.feedbackCellStatus == FeedbackCellStatusExpanded) {
return FeedbackHeightExpanded;
} else if (self.feedbackCellStatus == FeedbackCellStatusDone) {
return FeedbackHeightDone;
}

how to increase tableView height with scroll

I have tableView cell at bottom that have view that I will see if I tap on this cell.
Then I do it I increase contentSize and then scroll to this cell. But I can not scroll to top on boot ton position. I happens like tableView have default size of scrolling and it can not be increased. My code
if (indexPath.row == 29)
{
CGSize size = self.tableView.contentSize;
size.height += 100;
self.tableView.contentSize = size;
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
First in heightForRowAtIndexPath: increase the height of the that cell if the cell is tapped like:
if(cellIsSelected){
return height+100;
}else{return height;}}else{return height;}
and reload the tableview data.
scroll the tableview.

UITableView jumps to the top when a cell is deleted and then goes back to his position

I'm trying to code a feed so it's just a basic UITableView with one row per section and as many sections as posts.
If I select a cell the tableView displays a cell in the row 1 in the same section than the selected cell, and if I re-select the cell or scroll the tableView the row 1 is deleted.
The problem is that if i select the first cell and then scroll to the bottom (like a pull to refresh), the tableView scroll while the animation of the deleting cell is done then at the end of the animation jumps to the top of the screen and then return where he was.
I don't know if that was clear.
Here's my scrollViewDidScroll
def scrollViewDidScroll(scroll_view)
return if #can_scroll_comments
hide_comments unless #comment_triggered_for == -1
end
def hide_comments
feed_view = layout.get(:feed_view)
section = #comment_triggered_for
#comment_triggered_for = -1
feed_view.deleteRowsAtIndexPaths([NSIndexPath.indexPathForRow(1, inSection: section)], withRowAnimation: UITableViewRowAnimationFade)
end
in objective-c it should be something like
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (_can_scroll_comments) {
return ;
}
if (_comment_triggered_for != -1) {
[self hide_comments];
}
}
- (void)hide_comments: {
section = _comment_triggered_for
_comment_triggered_for = -1
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:section]] withRowAnimation:UITableViewRowAnimationFade];
}
Thanks for the help :)

Resources