I am using the collection view and i am trying to make infinite scrolling in collection view using the following method:
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
if (infoArray.count-1 == self.indexPathForVisibleCells.row) {
[self.collection_View scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO]; // [self.collection_View scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}
}
When i am using this method, when it comes at last cell and then it moves to the cell with index 1 not to the cell with index 0. Can someone explain this behaviour and give a possible solution to this ?
you must try this-
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]
atScrollPosition:UICollectionViewScrollPositionTop
animated:YES];
for more please refer following link-
http://www.tuicode.com/question/58707ef3c116a36d35c56c21
Related
//Scroll to top
NSIndexPath *index=[NSIndexPath indexPathForRow:0 inSection:0];
[self.collectionview scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
I tried this code but it is not working
[self.collectionview setContentOffset:CGPointMake(0,250) animated:YES];
with this code also the same result only
EDIT:
I found this SO thread.
UITableView inserting section at top while scrolling
I have a tableView
I load it with 5 sections initially
Then I scroll up and when section 0 and row 0 is displayed, I load the next 5 sections and insert in table view using
[self beginUpdates];
[self deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
[self insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
[self insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
[self insertSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
[self endUpdates];
However my problem is the tableView gets position at section 0 and row 0 after the inserts
but I want to position at last row of last new section inserted (which is section 2 in code above)
I tried this both inside and outside begin/end updates
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2];
[self scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionBottom animated:YES];
But it does it abruptly.
After inserting sections you can also scroll the table view to the intended index path using
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2];
[UIView animateWithDuration: 1.0
animations: ^{
[tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionBottom animated:NO];
}
completion: ^(BOOL finished){
}
];
Note:
1) Insert the above code after [self endUpdates];
2) Please ensure that you keep the animated flag to NO using scrollToRowAtIndexPath: atScrollPosition: animated: while you are using it with UIView's animateWithDuration: animations: completion: API.
3) Also you can calculate the duration based on the distance (content size, height wise for vertical scroll), i.e duration can depend on the distance between source index path and destination index path.
Please try below code.
[self beginUpdates];
/*
Insert your sections
*/
[self endUpdates];
[self performSelector:#selector(scroll) withObject:nil afterDelay:0.1];
-(void)scroll
{
if([Your array count] > 0)
{
[YOURTABLENAME scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}
I Hope above code is work for you.
Thanks
I don't know why this doesn't work. indexOfIcon is correct, section is correct (checked with NSLog) If I select one everything is correct. But this line doesn't do a thing...why? If selected it should have a blue border. This works great while doing it "manually" but not with code..
- (void)viewWillAppear:(BOOL)animated
{
NSUInteger indexOfIcon;
if(self.mainCategory.icon){
indexOfIcon = [self.icons indexOfObject: self.mainCategory.icon];
} else {
indexOfIcon = 0;
}
[self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:indexOfIcon inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionBottom];
}
add
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.selected = YES;
and the cell will be selected.
The command [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:indexOfIcon inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionBottom]; tells the collectionView, that the cell is in selected state, but don't set the state of the cell.
After years selection seems to work fine (even in viewDidLoad:)
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
[_collection selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionLeft];
successfully triggers cell's -setSelected:
I want scroll table view cell on top after clicked on cell, It is working up to 1 and 2 section but after that cell hidden behind "Keyboard".
- (void)scrollCellOnTop:(NSIndexPath *)indexPath
{
if(IS_IOS7)
{
self.supplierDetailTable .contentInset = UIEdgeInsetsMake(SCREEN_X,SCREEN_Y, self.supplierDetailTable.bounds.size.height-50, 0);
}
else
{
self.supplierDetailTable .contentInset = UIEdgeInsetsMake(SCREEN_X, SCREEN_Y, self.supplierDetailTable.bounds.size.height*tableInsetFraction, 0);
}
[ self.supplierDetailTable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
Please give me a solution if you have.
In order to scroll to a row in table view, you should do like below instead:
NSIndexPath *showOnTop = [NSIndexPath indexPathForRow:row_index inSection:section_index];
[table scrollToRowAtIndexPath:showOnTop atScrollPosition:UITableViewScrollPositionTop animated:YES];
Try replace line:
[ self.supplierDetailTable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
with:
NSIndexPath *iP = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
[self.supplierDetailTable scrollToRowAtIndexPath:iP atScrollPosition:UITableViewScrollPositionTop animated:YES];
- (void) SelectFirstCell
{
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
}
Hello, i have this very simple method that is supposed to highlight the first Table Cell, but it is not working, the method does get called, and the values do match, Row 0 inSection 0 does exist.
But it does not get highlighted ?
If it makes any difference it is a table created by the Tapku Calendar View
Its worth mentioning that i am very new to Obj C
Try this:
[self.yourTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
Try this:
- (void) SelectFirstCell
{
NSIndexPath * firstElement = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:firstElement];
[self.tableView selectRowAtIndexPath:firstElement animated:NO scrollPosition:UITableViewScrollPositionTop];
}
Hope it help!
you can use this
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[tableView selectRowAtIndexPath:indexPath animated:TRUE scrollPosition:UITableViewScrollPositionNone];
[[tableView delegate] tableView:tableView didSelectRowAtIndexPath:indexPath];