I want to scroll my collection view to the next cell after the user has finished editing a textfield.
My textfield delegate methods are getting called, and I can scroll the collection view at the press of a button, the two combined together don't work though.
-(BOOL)textFieldShouldReturn:(UITextField*)textField {
[textField resignFirstResponder];
NSArray *visibleItems = [self.collectionView indexPathsForVisibleItems];
NSIndexPath *currentItem = [visibleItems objectAtIndex:0];
NSIndexPath *nextItem = [NSIndexPath indexPathForItem:currentItem.item + 1 inSection:currentItem.section];
[self.collectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
return YES;
}
Any ideas?
if you want to scroll when you you finish editing a textfield than use textFieldDidEndEditing method.
Try this code...
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSArray *visibleItems = [self.collectionView indexPathsForVisibleItems];
NSIndexPath *currentItem = [visibleItems objectAtIndex:0];
NSIndexPath *nextItem = [NSIndexPath indexPathForItem:currentItem.item + 1 inSection:currentItem.section];
[self.collectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
}
Related
I have a UICollectionView. I want to scroll to the bottom when the view appears. How to do this?
Can someone help me out on this?.
Thanks in advance.
You can use the below code to scroll a UICollectionView programmatically to the bottom
NSInteger section = [self numberOfSectionsInCollectionView:collectionView] - 1;
NSInteger item = [self collectionView:collectionView numberOfItemsInSection:section] - 1;
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section];
[collectionView scrollToItemAtIndexPath:lastIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
NSIndexPath *lastItem = /*Indexpath of last item*/;
[self.collectionView scrollToItemAtIndexPath:lastItem atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
you this code to scoll down.
NSArray *visibleItems = [self.collectionView indexPathsForVisibleItems];
NSIndexPath *currentItem = [visibleItems objectAtIndex:0];
NSIndexPath *nextItem = [NSIndexPath indexPathForItem:currentItem.item + 1 inSection:currentItem.section];
[self.collectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
set the contentoffset.y of your collection view object.
I have UICollectionView that has two UICollectionResuabeView(Custom View) and one UICollectionViewCell. The form a single entity in UICollectionView. I put Delete button on one of UICollectionResuabeView and by pressing it want to delete the row (if i call it right).
This is the screenshot form Storyboard:
I tried getting section of it my many means, but non works. Guess I just dont have enough knowledge of how UICollectionView works. Here is some code I tried:
I know its I mess, but at least I was trying ))
I ve tried this toget indexPath, but none works.
-(IBAction)deletePressed:(UIButton* )sender{
RecipeCollectionHeaderView *contentView = (RecipeCollectionHeaderView *)[sender superview];
RecipeViewCell *cell = (RecipeViewCell *)[contentView superview];
cell = (RecipeViewCell *)[contentView superview];
// determine indexpath for a specific cell in a uicollectionview
NSIndexPath *editPath = [self.collectionView indexPathForCell:cell];
NSInteger rowIndex = editPath.row;
NSInteger secIndex = editPath.section;
// NSIndexPath *indexPath = nil;
// indexPath = [self.collectionView indexPathForItemAtPoint:[self.collectionView convertPoint:sender.center fromView:sender.superview]];
// NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
// NSSet *touches = [event allTouches];
//
// UITouch *touch = [touches anyObject];
//
// CGPoint currentTouchPosition = [touch locationInView:self.collectionView];
//
// NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint: currentTouchPosition];
NSLog(#"IndexPath CollectionView %ld", (long)rowIndex);
}
I also tried make it work like in this post, but it does not give a result:how to access from UICollectionViewCell the indexPath of the Cell in UICollectionView
Thank you all!
EDIT!
I found this works well:
-(IBAction)deletePressed:(UIButton* )sender{
[self.ordersArray removeObjectAtIndex:0];
[self.collectionView reloadData];
}
The only thing I need now is to get section of UICollectionView by UIBotton that been pressed, to delete section I want. How to do it?)
You also need to remove item from the array and UICollectionView as well.
Try to use this code.
-(void) deleteRow:(NSInteger)index {
[self.contentView performBatchUpdates:^{
[arrayForRows removeObjectAtIndex:i];
NSIndexPath *indexPath =[NSIndexPath indexPathForRow:i inSection:0];
[self.contentView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
} completion:^(BOOL finished) {
}];
}
For cell selection use this code snippet:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//Item position that you would like to delete.
indexPath.row;
}
I have a button that should call the delegate method of tableview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
when that button is tapped.
Can anyone tell me how to do this.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
A button won't be able to automatically call that method because it has no concept of a table view or index path, but if you create your own method you can call that method directly
- (void)buttonAction:(id)sender
{
NSIndexPath *path = //path for row you want to select
[self tableView:self.tableView didSelectRowAtIndexPath:path];
}
[self tableView:_myTbl didSelectRowAtIndexPath:[NSIndexPath indexPathForItem:_rowNeeded inSection:_sectionNeeded];
//call method, like user selected needed cell
[_myTbl selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
// show selected cell in UI
If you want that when user press the button on your TableViewCell then you fire the Action what's happen when a cell is tapped , then you can do the following.
In your cellForRowAtIndexPath method , add a Gesture Recogniser on your button. Like this ,
cell.myButton.userInteractionEnabled = YES;
cell.myButton.tag = indexPath.row ;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[tap setNumberOfTouchesRequired:1];
[tap setNumberOfTapsRequired:1];
[tap setDelegate:self];
[cell.myButton addGestureRecognizer:tap];
Here is the handleTap method
- (void)handleTap:(UITapGestureRecognizer *)recognizer
{
NSLog(#"Handle Tap method");
NSLog(#"Row Index : %d" , recognizer.view.tag);
int row_index = recognizer.view.tag ;
// Perform your Action woth row_index
}
And don't forget to add in your header file. Hope it helps.
For select entire row in table view cell check boxes :
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
take "i" value as for loop
i am stuck at this .please help me in sorting out this issue.
this is my image now i have 5 different rows in the table.
if u click on first textfield after editing the first field then i want done button click to goto the second field and open textview and textfield simulatneously(as shown in the image).
The Category Filed is having picker view,The Name field (shown in image),Location is having same as image,price is having a action sheet.
Now i have added code for action sheet of Price Field .Can somebody help me achieve the required.Thanks for help... :)
i am doing something like this but it is not yet worked for me.
the code is:
- (void) textFieldDoneTouched
{
[self.site setValue:textField.text forIndex:selectedRow];
[UIView animateWithDuration:0.3
animations:^{
CGRect f = textFieldContainerView.frame;
f.origin.y = self.view.frame.size.height;
textFieldContainerView.frame = f;
}];
[textField resignFirstResponder];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:selectedRow inSection:0];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
self.tableView.userInteractionEnabled = YES;
[self.view endEditing:TRUE];
[self validateSiteData];
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[self textFieldDoneTouched];
return YES;
}
#pragma mark -
#pragma mark UITextViewDelegate
- (void) textViewDoneTouched
{
[self.site setValue:textView.text forIndex:selectedRow];
[UIView animateWithDuration:0.3
animations:^{
CGRect f = textViewContainerView.frame;
f.origin.y = self.view.frame.size.height;
textViewContainerView.frame = f;
}];
[textView resignFirstResponder];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:selectedRow inSection:0];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
self.tableView.userInteractionEnabled = YES;
[self validateSiteData];
}
#pragma mark - UIActionSheetDelegate
- (void) actionSheet:(UIActionSheet *)actionSheet1 clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0) {
self.site.price = #"Free";
// NSIndexPath *indexPath = [NSIndexPath indexPathForItem:4 inSection:0];
// [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:100];
//[textField resignFirstResponder];
// [textView becomeFirstResponder];
}
else if(buttonIndex == 1) {
self.site.price = #"Paid ($)";
// NSIndexPath *indexPath = [NSIndexPath indexPathForItem:4 inSection:0];
// [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:100];
// [textField resignFirstResponder];
//[textView becomeFirstResponder];
}
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:selectedRow inSection:0];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self validateSiteData];
}
So, you'd like the iPhone's return key to go to the next field, like pressing Tab on a computer keyboard?
One minor note: I'd recommend using the default grey "return" key, not the blue "Done" key in your screenshot. "Done" is for when the user is done filling out the form and the keyboard should disappear; "return" is for going to the next field or row of text, as in Apple's Contacts app.
Like Apple's Contacts app, I'd also recommend that your UITextFields be in the same UITableViewCells as their labels (Category, Name...). I find the text field directly above the keyboard in your screenshot a little confusing. If you do that, then the following implementation of textFieldShouldReturn will make the next cell's text field into the first responder.
- (BOOL) textFieldShouldReturn:(UITextField*)textField {
// Determine the row number of the active UITextField in which "return" was just pressed.
id cellContainingFirstResponder = textField.superview.superview ;
NSInteger rowOfCellContainingFirstResponder = [self.tableView indexPathForCell:cellContainingFirstResponder].row ;
// Get a reference to the next cell.
NSIndexPath* indexPathOfNextCell = [NSIndexPath indexPathForRow:rowOfCellContainingFirstResponder+1 inSection:0] ;
TableViewCell* nextCell = (TableViewCell*)[self.tableView cellForRowAtIndexPath:indexPathOfNextCell] ;
if ( nextCell )
[nextCell.theTextField becomeFirstResponder] ;
else
[textField resignFirstResponder] ;
return YES ;
}
Let me know if you'd like me to post the whole project.
I want to add cells into UItableView like in mail.app on ipad. In my cells I have UITextFields, and I have change this method to set focus
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath{
NSLog(#"%d",newIndexPath.row);
UITextField* field = (UITextField *) [tblView viewWithTag: newIndexPath.row];
int l_cellNum = field.tag;
if(l_cellNum == 1 && isAddedNewRow == FALSE){
isAddedNewRow = TRUE;
[cellsArray insertObject:#"CC" atIndex:1];
[cellsArray insertObject:#"BCC" atIndex:2];
CGRect tvbounds = [tblView bounds];
[tblView setBounds:CGRectMake(tvbounds.origin.x,
tvbounds.origin.y,
tvbounds.size.width,
tvbounds.size.height)];
NSIndexPath *path1 = [NSIndexPath indexPathForRow:1 inSection:0];
NSIndexPath *path2 = [NSIndexPath indexPathForRow:2 inSection:0];
NSArray *indexArray = [NSArray arrayWithObjects:path1,path2,nil];
[tblView insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationTop];
[tblView reloadRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[field becomeFirstResponder];
}
But when I click to one of the cell i add 2 cells (like mail.app) but my tags for textFields do not updates, and I cant to set focus on it =(. If I call reloaddata method it calls -cellForRowAtIndexPath with this new cells at the beggining and after for all cells, and it will not work too.
It sounds like what you're looking for is a built-in style of cells called UITableViewStyleSubtitle, where you can customize the subtitle just like the iPad Mail.app. Take a look at this page in the iOS documentation. Then, you can set the subtitle of the UITableViewCell to the text that you need.
Additionally, the iOS doc has more information about customizing table view cells.