Selecting UITableViewCell programmatically? - objective-c

I want to make a now playing button for my radio application. So would like to know how can I select a row programmatically I thought of calling didSelectRowAtIndexPath method but apparently we can't call it.

didSelectRowAtIndexPath is the method from delegate and it's called when you touch (select) the row in UITableView
Use below method to selct a row on your UITableView instance.
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
See Sample code.
[myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];

There is tableView method like [tableView selectRowAtIndexPath:anIndexPathObject animated:BOOL-Flag]; .By calling this method you can select the row programmatically. But you also need to create an index path object.

Related

get indexPath for customCell in TableView

I have a UITextField in a customCell. I want to be able to get the text that user inputs in it. In my customCell.m file I can get the text that is inputed with
[self.aTextField addTarget:self action:#selector(aTextFieldEditing:) forControlEvents:UIControlEventEditingChanged];
but I cannot get the indexPath so I can save it to the NSMutableArray that holds all the UITextFields.
In my TableView I cannot get the didSelectRowAtIndexPath to run. I also cannot get the textFieldDidBeginEditing method to output anything. If it helps I have a TableView with sections.
My idea at the moment is to get the indexpath.row and indexpath.section from the tableView and save them as an extern so I can access it in the customCell.m
I would be grateful for any ideas or specific examples of how I could do this. Or a different cleaner way to accomplish what I want.
You should be able to get the index path using the -indexPathForRowAtPoint: method on UITableView.
You can do something like this (not tested):
- (void)aTextFieldEditing:(UITextField *)textField {
CGPoint convertedPoint = [textField.superview convertPoint:textField.center toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedPoint];
// Use your index path...
}
Edit:
In the case your cell is receiving the edit events, you can always define a protocol and make your view controller a delegate of the cell. You could then call this delegate method from your aTextFieldEditing method and pass the cell. Now that you have the cell you can call -indexPathForCell on your table view. If none of this makes sense, look into the delegate pattern. It's very common in Cocoa/Cocoa Touch and is very well documented online.

Cocoa Touch: How can I update the table view everytime I tap on a tab of tabbar?

I'm using iOS 8 and Objective c.
If I do a change in the values in one view, I can't see the reflection on my other tabviews. But if I re-run the app, I can see the reflection on the other tabs. Sometimes it works, sometimes it doesn't. How can I make sure that when I tap on a tabview, it will be reload according to the database?
I'm not sure what you are trying in your code, but you can try to implement the viewDidAppear method with a reloadData in your tableView:
- (void)viewDidAppear:(BOOL)animated{
[self.tableView reloadData];
}
[tableView reloadData] is the one you are looking for , this UITableView method refreshes the table view by calling its delegate and data source methods again to get fresh data.
I solved it!
Here is my code:
"TheMaster" is the main tabview. Here selectedIndex can be 0,1,2.... according to the placement of the tab. The leftmost tab has the index 0, and after that 1,2, and so on.
UITabBarController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"TheMaster"];
controller.selectedIndex=0;
[self presentViewController:controller animated:YES completion:nil ];

How can I figure out the indexPath of a cell I'm not pushing using didSelectRowAtIndexPath?

I've got a UITableView with custom cells inside, which trigger a detail view when a UIButton in them is tapped. Tapping the cell itself does not trigger the segue, meaning didSelectRowAtIndexPath is never called.
Before implementing this, I was using didSelectRowAtIndexPath to set up the object being passed to the detail view via its indexPath in my NSFetchedResultsController's fetchedObjects array. Since I'm now not using didSelectRowAtIndexPath, I can't access the indexPath there.
I've already tried using the tag of the button, but since it can't store an indexPath, this doesn't work when I call objectAtIndexPath before the segue. I've also tried accessing the button's superview to get its indexPath, but that didn't work either, was always nil.
How else can I know which UIButton was pushed, so that I can pass the correct object to my detail controller?
Edit: I thought of the idea of adding a UIButton category that adds an indexPath property, but I'm having trouble doing this since it expects a method to go along with it. Would this approach work?
Follow the superview of the button (or the superview of superview of the button) to get the Cell and get the indexpath of the cell from tableview
I fixed by adding the following to the method called when I tap the button:
UIView *contentView = (UIView *)[sender superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell: cell];
_quote = [self.fetchedResultsController objectAtIndexPath: indexPath];
[self performSegueWithIdentifier: #"ViewDetail" sender: nil];

didSelectRowAtIndexPath not called on tableView

I am using tableView in a viewcontroller1 and implementing a didSelectRowAtIndexPath for moving to viewController2 with some data and displaying it on a tableView on viewController2...for now everything is ok, but then when I implement the code on viewController2:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath) {
// some code
}
After I selecting a row its selected and colored in blue but never stop's in the method didSelectRowAtIndexPath.
What can be the problem?
You should set not only the data source of the table view to your view controller, but the delegate also:
tableView.delegate = self;
You need to set the delegate of your tableView, either in your storyboard or XIB file, or programatically.

What method needs implemented to get an action from the UITableViewCell click / touch?

I'm new to the UITableView and noticed that w/out implementing a method the UITableViewCell simply turns blue when it's touched. What method would I need to add to get some action from this touch directly?
For example, when the cell is clicked I want to pass off the index to another method that will in turn do the same action that is completed by the accessory button click event.
Thank you in advance
You need to implement didSelectRowAtIndexPath method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// indexPath.row tells you index of row tapped
// here do whatever your logic
}
didSelectRowAtIndexPath
Link here
You need to assign a UITableViewDelegate to the table view, and implement one or more of the following 2 methods in the delegate class, depending on when you want the event to be fired:
-tableView:willSelectRowAtIndexPath:
-tableView:didSelectRowAtIndexPath: