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.
Related
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.
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];
}
I want to increase tableview cell and tableview height based on content.
Suppose tableview contain 2 record and his 1st cell height is 100 and 2nd cell height is 25 then tableview height should be 100+25=125.
How to achieve this functionality?
Thanks in advance.
You can definitely do that,
First make sure your constraint of cells subView must set to top to bottom in order to calculate the height required for the cell.
Make sure your delegated are set as below
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
Set height constraint of your tableView and make outlet of that constraint.
Add below method to your class where you want to resize your tableView dynamically.
- (void)adjustHeightOfTableview
{
CGFloat height = self.tableView.contentSize.height;
//CGFloat maxHeight = self.tableView.superview.frame.size.height - self.tableView.frame.origin.y;
/*
Here you have to take care of two things, if there is only tableView on the screen then you have to see is your tableView going below screen using maxHeight and your screen height,
Or you can add your tableView inside scrollView so that your tableView can increase its height as much it requires based on the number of cell (with different height based on content) it has to display.
*/
// now set the height constraint accordingly
self.constraintHeightTableView.constant = height;
//If you want to increase tableView height with animation you can do that as below.
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
}
Call this method when you are ready with the dataSource for the table, and call the method as
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
//In my case i had to call this method after some delay, because (i think) it will allow tableView to reload completely and then calculate the height required for itself. (This might be a workaround, but it worked for me)
[self performSelector:#selector(adjustHeightOfTableview) withObject:nil afterDelay:0.3];
});
If you are running iOS 8+,
You can use:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80 // your desired or expected height
properties.
for this to take effect you should not have any height set in heightForRowAtIndexpath
You should set the cell constraints i.e., constraints for the elements present inside cell, so the set constraints are enough for the tableviewcell to calculate it's height in run time
Solution in swift 3
Step 1.
Set the top, bottom, leading and trailing constraints (do not make it constant height, but do set a constant height constraint as of now).
Now we gonna change this height dynamically based on the number of cells and its height.
Step 2.
Drag an outlet of that height constraint to the class, and copy this function anywhere in the class.
func adjustHeightOfTableview() {
let height: CGFloat = tableview.contentSize.height
self.outLetOfTheHeightConstraint.constant = height
}
Step 3.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
DispatchQueue.main.async {
self.tableview.reloadData()
self.perform(#selector(self.adjustHeightOfTableview))
}
}
self.tableView.frame = CGRectMake(self.tableView.origin.x, self.tableView.origin.y, self.tableView.frame.size.width, 125);
I am using collection view for the first time and I'm not sure how to do something..
I have a UICollectionView with 12 cells. I set the collectView to scroll horizontally only and cells are lined up next to each other. I also turned on paging so I could use UIPageControll to indicate scrolling is active.
I want the collection view to only show four cells on the screen at any time. When the view loads, I get four cells, no problem. However when I scroll horizontally, I get 4 and a half cells. never just four.
Is there a way to tell the collection view only to show four cells at a time?
you can statically add number of cell(items)in collection view,if not require dynamic.
here I am using Scroll Direction Horizontal you can do it same way in vertical.
hope this will help
you can do this way also. Just copy this code into your view controller and make some changes.
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSIndexPath *indexPath;
for (UICollectionViewCell *cell in [self.collectionView visibleCells]) {
indexPath = [self.collectionView indexPathForCell:cell];
NSLog(#"%#",indexPath);
}
UICollectionViewCell *cell =(UICollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
//finally get the rect for the cell
CGRect cellRect = cell.frame;
self.collectionView.contentOffset = CGPointMake(self.collectionView.contentOffset.x, cellRect.origin.y);
}
As Marc said, you could simply control the size of your collection view.
If changing the size is not practical, then you can set content inset on the collection view.
CGFloat cellWidth = … // Cell width
CGFloat collectionViewWidth = … // Collection View Width
CGFloat desiredCollectionViewWidth = cellWidth * 4.0;
CGFloat horizontalInset = collectionViewWidth - desiredCollectionViewWidth;
// To center the collection view
UIEdgeInsets inset = UIEdgeInsetsMake(0, horizontalInset/2, 0, horizontalInset/2);
self.collectionView.contentInset = inset;
// Or, to left justify the collection view
UIEdgeInsets inset = UIEdgeInsetsMake(0, 0, 0, horizontalInset);
self.collectionView.contentInset = inset;
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);
}
}