UITableView Delegate - ios

I have a table view cell with a "Show More" button. On clicking the button I want the delegate
-tableView:didSelectRowAtIndexPath: to get called ?
Can someone tell me the difference between the following 2 statements:
[self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
and
[[self.tableView delegate] tableView:self.tableView didSelectRowAtIndexPath:indexPath];

There shouldn't be any difference between those two statements, assuming that self is the delegate of the table view, and you have a property called tableView in your controller that points to the table view (connected to the table view in IB if it's an outlet, or assigned to the table view in code if you created the table view there).

Related

Storyboard segue gets called before UITableView's didSelectRow

I have a storyboard with segue from a table cell. I want to set some properties with some data when a row gets selected so I do the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[ProperyManager sharedPropertyManager]setSelectedRow:[verseIds objectAtIndex:indexPath.row]];
[[ProperyManager sharedPropertyManager]setID:[poemIDs objectAtIndex:indexPath.row]];
[[ProperyManager sharedPropertyManager]setRowToReturn:[NSString stringWithFormat:#"%i",indexPath.row]];
}
The problem is, the view controller lifecycle methods (viewWillAppear etc.) of the destination view controller get called before the didSelectRow method above, because the segue pushes the view before the delegate method is executed.
How can I get around this?
Rawkode's answer is one good solution - an alternative is that, in prepareForSegue:, you can access the selected row of the table view (the sender argument will be the table view cell, you can then do [self.tableView indexPathForCell:(UITableViewCell*)sender] to get the index path) and set up whatever you need at that point.
Don't create the Segue from the Cell to the new VC, instead set the Segue from the old VC to the new VC and give the segue an identifier.
Then within
didSelectRowAtIndexPath you can call
[self performSegueWithIdentifier:#"Segue" sender:self]

Force tableView reloadData from Modal Dismissal?

When my application launches, a table view is present and uses the information in two arrays (countdowns and countdown_info) to populate the cell title and description in the table view respectively.
A modal can be opened and used to create a new countdown. So far, it saves the data in a local file, closes, and then returns to the front controller, but the table is not updated with the new data.
How can I get the new information added to the table? The UITableView is named as *countdown_table
Thanks!
EDIT: The return code looks like this:
...Above Saves the File...
// The following generates a warning. ClockworkViewController is the main view controller.
//[[ClockworkViewController countdown_table] reloadData];
[[self parentViewController] dismissModalViewControllerAnimated:YES];
Load the newly added value to the array
Call [tableview reloadData] in your table view controller's viewWillAppear method.
Are you calling the [yourTableView reloadData] function? You should call this either when the modal view is saved, or when the table view becomes active again. This will then check all of the tableView's methods for number of rows, number of sections, etc, and reload the entire table.
You may explicitly call [table reloadData] when you dismiss the modal view
If you're using Interface Builder to perform your presentations/dismissals, you can very easily reload the ParentVC's tableView from the ModalVC's Exit Segue (transitioning back to the ParentVC, of course).
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"YourDismmssSegueID"]) {
ParentVC *vc = segue.destinationViewController;
// perform any needed data manipulation here
[vc.tableView reloadData];
}
}

How to refresh/reload a UITableView

I have a table View and a uibutton.
When i click on uibutton, it will display a model view control using presentModelviewcontroller method.
I have a text field and save button in the presentmodelview.
Please let me know how to reload table view when the user click's on presentmodelview save button
If you need the presentModalViewController to be removed and then the tableView to be reloaded then use this
[yourTableView reloadData];
on the viewWillAppear of the initial view.
First add the item to the array, then reload your table view.
Assuming you declared all properties needed including the array used by the data source.
#property(nonatomic, retain)NSMutableArray *modalArray;
The IBAction on Save button should include the following code:
//Add the item to the array
[self.modalArray addObject:[NSString stringWithFormat:#"%#",txtField.text]];
//Rest of your code, dismiss the modal view
Once dismissed, you need to reload the table view to show up the recent value saved on the modal view.
[self.yourTableView reloadData];
reloadData will call implicitly the data source method tableView:cellForRowAtIndexPath: which will loop the array again and so show you the new value.
In a nutshell,
YourViewControllerClass * vc = [[YourViewControllerClass alloc]init];
[self presentViewController:vc animated:NO completion:nil];
Hope this helps :)
when you want reload tableview please use below line
[self.tableView reloadData];

How to get reloadData to work on UITableView

I have a UITableView that loads from a NIB file. The controller for the screen is a subclass of UIViewController that conforms to the UITableView delegate protocols. This is the 2nd screen in a stack of views managed by a UINavigationController.
In my viewWillAppear for the offending view I run two NSFetchRequests and update 2 of the 3 sections in my table with results from those NSFetchRequests. At the bottom of viewWillAppear I call
[self.myTable reloadData];
myTable is an IBOutlet to the UITableView for the screen.
For whatever reason the table doesn't reload the data and none of the delegate methods for the table get called when I come back up to it from views deeper in the Navigation Controller hierarchy.
How do I get the table to reload?
Thanks.
Instead of self.tableView, use:
[myTable reloadData];
So if you defined your IBOulet as myTable, then you need to use that name instead.
self.myTable would work if you initialized myTable like in your header:
self.myTable = [[UITableView alloc] initWithFrame:CGRectZero];
Have you also linked the UITableView to data source and delegate outlets in the nib file? You have to link these two in addition to the IBOutlet.

Make UITableView selection before view appears

I'm building a detail view for a UITableView. The detail view contains a UITableView which has two rows, and I want the first one to be selected when the detail view is opened. I have tried to use the method [selectRowAtIndexPath:animated:scrollPosition] in [viewWillAppear]:
- (void) viewWillAppear:(BOOL)animated {
[[self tableView] selectRowAtIndexPath:[NSIndexPath indexPathForRow:kStartDateRow inSection:kSection]
animated:NO
scrollPosition:UITableViewScrollPositionNone];
}
However, the selection is not affected by this, and using it in [viewDidAppear] means the selection changes once the view has moved into place. How can I make the selection before the detail view appears on screen?
You can see an example of the behaviour I want in the Settings app. Go to General > Date & Time > Set Date & Time.
You don't mention any error but try doing [[self tableView] reloadData] before the selectRowAtIndexPath line.

Resources