reload data uitableview from another view - ios

I want to reload data of UITableview from another UIView.
(the UITableview is in ListNoteViewController)
the call I do from DetailViewController (the other uiview) is :
ListNoteViewController *controller = [[ListNoteViewController alloc] initWithNibName:#"ListNoteViewController" bundle:[NSBundle mainBundle]];
[controller.TableNotes reloadData];
but nothing happens. Please, someone can help me ?
Advanced thanks

You can use delegates.
in DetailViewController.h
#property (nonatomic, strong) id Delegate;
In ListNoteViewController.m
-(void)myTVReloadMethod {
[myTable reloadData];
}
And when you are going to DetailViewController
DetailViewController *dvc = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
dvc.Delegate = self;
[self.navigationController pushViewController:dvc animated: yes];
In any method in DetailViewController
[delegate performSelector:#selector(myTVReloadMethod) withObject:nil];

Nothing will happen, because you are trying to reload a tableview which is not yet created!
The UI elements like UITableView, UILabel etc will be created only after the view of the particular UIViewController is loaded. Here you have just initialised the viewcontroller only.
So you should write this in
-(void)viewDidAppear:(BOOL)animated
{
[self.TableNotes reloadData];
}
OR
-(void)viewWillAppear:(BOOL)animated
{
[self.TableNotes reloadData];
}
of your ListNoteViewController.
This is the problem with your code. And in case, if the ListNoteViewController is already in stack,ie, if you have already loaded the same viewcontroller and is now trying to reload the tableview on it, then you can use custom delegates to achieve the same.
you can see how to add a delegate here.

Related

uitableview detail view programmatically

Am new to objective c,I need to display the Json response in uitableview cell programmatically.And onclick of particular row display its details in next page in table view cell.please give some info regarding this.
if You are using storyboard, you can implement this way.
Implement bellow line in your DetailViewController.h file.
#property (strong, nonatomic) id responseData;
Then implement bellow method
-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
DetailViewController *eventDetailsViewController = [storyboard instantiateViewControllerWithIdentifier:#"DetailViewController"];
eventDetailsViewController.responseData = YourResponseData;//pass response data to detail view
[self.navigationController pushViewController:eventDetailsViewController animated:YES];
}

ViewDidLoad not called from didSelectRowAtIndexPath splitviewController

I am trying to load a single (storyboard id:ClaimReportView) ViewControllers with diff data depending upon the rows clicked on the table view of MasterVC. But the viewDidload is called only once for the VC and hence if i trying to click on diff rows in table view it doesn't call the viewdidload.
.M file
#interface LAMasterViewController ()
{
NSArray *_claimReports;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
LAClaimReport *record = [_claimReports objectAtIndex:indexPath.row];
if ([record.submitted isEqualToNumber:#0] )
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
self.claimReportViewController = [storyboard instantiateViewControllerWithIdentifier:#"ClaimReportView"];
[self.detailViewController.navigationController setViewControllers:[NSArray arrayWithObject:self.claimReportViewController] animated:YES];
self.claimReportViewController.claimReport = record;
}
}
.h file
#interface LAMasterViewController : UITableViewController
#property (strong, nonatomic) LADetailViewController *detailViewController;
#property (strong,nonatomic) LAClaimReportViewController *claimReportViewController;
#end
What am i missing here? i think i haven't pushed the VC in navigation Stack so ViewDidLoad not called? pls guide.
viewDidLoad is called once when your view is loaded in the first time so there will be no affect in tableView:didSelectRowAtIndexPath: method . Its better you can create your own method and call that method in the tableView:didSelectRowAtIndexPath: method that will work for you.
You can not call viewDidLoad method because it is viewController's life cycle method that can not be called manually, it will be called automatically when view is loaded.

Passing id from Parse table view to a detail view

(I know this has been asked before, unfortunately none of the related StackOverflow answers I've looked through and tried out while trying to figure this problem out have worked for me. Any help would be much appreciated.)
I'm building a simple news app. The opening screen grabs the latest news entry using Parse's API.
And when you tap the menu button, you get a UITableView of past news days:
I want to be able to pass the id from the selected Parse table cell back to the main view.
I'm using ECSlidingViewController for the sliding menu and a Storyboard for the various screens:
But because of the way I'm using ECSlidingViewController, I can't figure out how to pass data via segues like I otherwise would be able to, so I'm trying to just pass it via the view controllers with a custom init.
Here's my MainViewController.h code:
#interface MainViewController : UIViewController
#property (nonatomic) NSString *detailId;
- (id)initWithDetailId:(NSString*)theId;
#end
Here's my didSelectRowAtIndexPath code in MenuViewController.m:
MainViewController *mainVC = [[MainViewController alloc] initWithDetailId:#"test"];
[self.navigationController pushViewController:mainVC animated:YES];
Here's MainViewController.m's initWithDetailId:
- (id)initWithDetailId:(NSString*)theId
{
NSLog(#"initWithDetailId");
self = [super initWithNibName:nil bundle:nil];
if (self) {
detailId = theId;
}
return self;
}
And here's MainViewController.m's viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"detailId equals: %#", self.detailId);
}
But all I get from the NSLog is "detailId equals: (null)". What am I doing wrong here? Thanks!
you have forgot to put self. in front of detailId = theId;
self.detailId = theId;
that is
- (id)initWithDetailId:(NSString*)theId
{
NSLog(#"initWithDetailId");
self = [super initWithNibName:nil bundle:nil];
if (self) {
self.detailId = theId;
}
return self;
}
there is one another method of doing this:
MainViewController *mainVC = [self.storyBoard instantiateViewControllerWithIdentifier:#"yourIdentifier"];
mainVc.detailId=#"test";
[self.navigationController pushViewController:mainVC animated:YES];

Show DetailView from UITableView

I have an app in portrait, in my View Controller (UIViewController, not a UITableViewController) there is a table (UITableView) that only occupies half the screen.
I set the delegate and datasource of my table view to my view controller.
Now, I would like to show a detail view when i select any cell of my table. Here is what I tried but nothing appears:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"SELECTED");
//ChangeView
DetailViewController *dvController = [[DetailViewController alloc]
initWithNibName:#"DetailViewController" bundle:nil];
[self.navigationController pushViewController:dvController animated:YES];
dvController = nil;
}
Any idea? Is this solution better than a popup solution?
You Should check your tableview in nib file it datasource and delegate is connected or not?
just connect both with file owner.
and if you are using uiview controller then use present model view controller not push modelview controller.
DetailViewController *dvc=[[DetailViewController alloc]initWithNibName:#"DetailViewController" bundle:nil];
[self presentModalViewController:dvc animated:YES];
Put [NSBundle mainBundle] in place of nil. Hope it helps. Tell me what happens!
Also replace dvController=nil; with [dvController release];
EDIT:
Replace ur init function with this one:
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
return self;
}
initialize navigation controller. becauze some time it is due to ui component which has not initialized. but remember that you may have to face some other UI issues
You should check if didSelectRowAtIndexPath is really called or not when you select a table cell (of not, your delegate may not be set correctly), then if
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
returns nil or something meaningful with a debugger, trying to close in on the problem. Also, #M.A.Khan was right, you should remove
dvController = nil;
and replace it with
[dvController release];
giving up the reference in your main view.
Edit:
Your code asumes that beneath your UIViewController, there is a UINavigationController because you're using [self.navigationController pushViewController]. If there isn't, no wonder your view does not get pushed onto something that doesn't exist.
is the viewcontroller with tableview in a navigationcontroller?if not then first you have to create a navigationcontroller with the current viewcontroller as the rootviewcontroller.then try this code u are using now

calling didSelectRowAtIndexPath from other ViewController not responding iPad

I want to change the selected row in the table (in MasterViewController) from a button on a detailViewController.
I know that I cannot call didSelectRowAtIndexPath because it is a delegate method. I tried using selectRowAtIndexPath but that does not invoke the didSelectRowAtIndexPath nor the willSelectRowAtIndexPath method. As suggsted here,
https://stackoverflow.com/a/7496926/1050722 it is possible to some new method and then call that method from the other ViewController (detailViewController), but how should that method look like?
Here is some code:
MasterViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self pushDetailViewController:indexPath];
}
-(void)pushDetailViewController:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
self.detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
}
[self.navigationController pushViewController:self.detailViewController animated:YES];
NSLog(#"pushDetailviewC and index %#", indexPath);
}
DetailViewController.m
-(IBAction)nextItem
{
MasterViewController *mVc = [[MasterViewController alloc] init];
[mVc pushDetailViewController:[NSIndexPath indexPathForRow:0 inSection:0]];
}
Can someone give me an example to fix this? Thanks.
EDIT: Here is the new code
MasterViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self pushDetailViewController:indexPath];
}
-(void)pushDetailViewController:(NSIndexPath *)indexPath{
if (!self.detailViewController) {
self.detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
}
self.detailViewController.mVc = self;
[self.navigationController pushViewController:self.detailViewController animated:YES];
NSLog(#"pushDetailviewC and index %#", indexPath);
}
DetailViewController.h
#import <UIKit/UIKit.h>
#import "MasterViewController.h"
#interface DetailViewController : UIViewController <UISplitViewControllerDelegate, UITableViewDelegate>
#property (strong, nonatomic) id detailItem;
#property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
#property (nonatomic, retain) MasterViewController *mVc;
DetailViewCOntroller.m
-(IBAction)test{
//MasterViewController *mVc = [[MasterViewController alloc] init];
[self.mVc pushDetailViewController:[NSIndexPath indexPathForRow:0 inSection:0]];
}
It does not call the NSLog part of the method and it is not selecting the row...
This answer is iPad specific as per your question.
The iPad version of the Master/Detail template uses a UISplitViewController two split the screen as you see it. This UISplitViewController instance keeps references to both of your navigations controllers. Which keep reference to your topmost view controllers. Here is sample code, do not use it verbatim except to test since it contains no error checking.
// Picking some arbitrary row
NSUInteger desiredRow = 3;//???
// The Master navigation controller is at index zero in the template, you can check in your app delegate's didFinishLaunching: method
UINavigationController *masterController = [self.splitViewController.viewControllers objectAtIndex:0];
// Next we want a reference to the topmost viewController again you shouldn't assume it's a UITableViewController
UITableViewController *tableController = (UITableViewController *)masterController.visibleViewController;
// Next we get the reference to the tableview in-question
UITableView *tableView = tableController.tableView;
// We translate our desired row to an index path
NSIndexPath *path = [NSIndexPath indexPathForRow:desiredRow inSection:0];
// We select the row , again this will not call didSelectRowAtIndexPath:
[tableView selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionNone];
// We call didSelectRowAtIndexPath: on the tableview's delegate
[tableView.delegate tableView:tableView didSelectRowAtIndexPath:path];
When I put this code in an IBAction method in the detail view controller the linked button performs the appropriate actions, both selects the row and pushes a VC, as if I had selected the row by touch.
In DetailViewController.h add a property to the object that points to the MasterViewController. You will also need to import the MasterViewController.h so something like
#import "MasterViewController"
...
#property (nonatomic, retain) MasterViewController *mVc;
...
Don't forget in DetailViewController.m to #synthesize mVc;
Now in your pushDetailViewController you add a reference to join the two objects
if (!self.detailViewController) {
self.detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
}
[self.detailViewController.mVc = self]; // New line
[self.navigationController pushViewController:self.detailViewController animated:YES];
Then in the DetailViewController you reference the object
-(IBAction)nextItem{
[self.mVc pushDetailViewController:[NSIndexPath indexPathForRow:0 inSection:0]];
}
I think another issue you were having was alloc'ing a new version of mVC each time you click the nextItem button. IOS will happily let you make lots of MasterViewController objects and makes a new one each time you alloc it. You wanted just to get a handle to your original MasterViewController.
Another approach would be to explore using the parent methods to reference the MasterViewController. But I wanted to show you how to do this with an explicit property since I thought it would be clearer.
Also, this may or may not fix all of your issues, but it hopefully at least shows you how to talk back to the actual MasterViewController.

Resources