How to call the delegate method of UITableView manually? - ios

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

Related

Scroll collection view after finished editing textfield

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];
}

Can not select UITableViewCell programmatically

I am trying to select a row when the table view is loaded. I've found other thread regarding this issue but they didn't solve my problem so I am asking again.
My tableview contains 5 row. And I am doing this:
if ([[dataSource objectAtIndex:indexPath.row] isEqualToString:self.status]) {
[self tableView:[self tableView] didSelectRowAtIndexPath:indexPath];
}
I've also tried cell.selected = YES; and cell.highlighted = YES;.
I am using the following code for custion selection view:
UIView *customColorView = [[UIView alloc] init];
customColorView.backgroundColor = [UIColor colorWithRed:180/255.0
green:138/255.0
blue:171/255.0
alpha:0.5];
cell.selectedBackgroundView = customColorView;
Please help me selecting a row programmatically. The row is selected when I tap on the row, but can not select programmatically.
Try with following code
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
Here you need to set indexPath for row that you want to be selected.
You can get this by calling UITableView delegate method which is
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}
inside this method you need to check every selected time that same cell are you not tapped.
if ([[myArray objectAtIndex:indexPath.row] isEqualToString:self.same])
{
[myTable selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
Try calling selectRowAtIndexPath on the table view object:
if ([[dataSource objectAtIndex:indexPath.row] isEqualToString:self.status])
{
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}

How to find cell indexPath that was selected when calling [tableView setEditing:NO]?

On a tableView that allows selection in edit mode, how do i find which was the selected cell when i call [tableView setEditing:NO animated:YES] and exit edit mode ?
There is no call to didDeselectRowAtIndexPath:, are there any other methods i overlooked ?
Edit for solution:
The cell never gets deselected, therefore indexPathForSelectedRow still returns the correct value. Fair enough.
you can use GestureRecognizer for this .
try this :
UISwipeGestureRecognizer *gestureObj = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(Method:)];
[gestureObj setDirection:UISwipeGestureRecognizerDirectionRight];
[self.tableview addGestureRecognizer:gestureObj];
-(void)Method:(UISwipeGestureRecognizer *)gestureRecognizer {
CGPoint p = [gestureRecognizer locationInView:self.tableview];
NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:p];
if (indexPath == nil)
{
[self setEditing:YES animated:YES];
NSLog(#"slide on table view but not on a row");
}
else
{
[self setEditing:YES animated:YES];
NSLog(#"slide on table view at row %d", indexPath.row);
}
UITableViewCell *cell = [self.MYArray objectAtIndex:indexPath.row];
}
The answer is that the row never gets deselected.
indexPathForSelectedRow is still valid before actually exiting the edit mode.

how to move to next cell/row in table view with textview

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.

Automatically cell selected UITableView

My UITableView opens via PopOverViewController , so How can I load one of these cells automatically after app did load ,
the cell selecting process on MainViewController
- (void)setDetailItem:(id)newDetailItem {
if (detailItem != newDetailItem) {
[detailItem release];
detailItem = [newDetailItem retain];
//---update the view---
label.text = [detailItem description];
}
}
and cell selecting in TableViewController :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
myAppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
appDelegate.viewController.detailItem = [list objectAtIndex:indexPath.row];
}
I use this code in TableViewController but does not work ! It means after press the the popOver button the code just highlight the cell !!
[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];
I used above code in different methods like viewDidAppear , viewWillAppear and didSelectRowAtIndexPath and ...
Thank you
When you call selectRowAtIndexPath:animated:scrollPosition:, tableView:didSelectRowAtIndexPath: is not called on the delegate.
From the selectRowAtIndexPath:animated:scrollPosition: reference:
Calling this method does not cause the delegate to receive a
tableView:willSelectRowAtIndexPath: or
tableView:didSelectRowAtIndexPath: message, nor will it send
UITableViewSelectionDidChangeNotification notifications to observers.
So, instead of just calling selectRowAtIndexPath:animated:scrollPosition::
[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];
you could call the delegate methods manually:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
if ([myTableView.delegate respondsToSelector:#selector(tableView:willSelectRowAtIndexPath:)]) {
[myTableView.delegate tableView:self.tableView willSelectRowAtIndexPath:indexPath];
}
[myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition: UITableViewScrollPositionNone];
if ([myTableView.delegate respondsToSelector:#selector(tableView:didSelectRowAtIndexPath:)]) {
[myTableView.delegate tableView:self.tableView didSelectRowAtIndexPath:indexPath];
}

Resources