I am working on a simple UI, but am running into trouble with addSubview calls made after a removeFromSuperview.
In my viewController I have an IBOutlet UITableView
#property (strong, nonatomic) IBOutlet UITableView *tableView;
Depending on the state of the data, I remove the tableview from my viewControllers view.
[self.tableView removeFromSuperview];
So far so good, the view is removed. When using the debugger, I can still see that my viewController has a valid handle to the tableview. The problem starts on the next line when I call addSubview.
[self.view addSubview:self.tableView];
[self.tableView setDataSource:studyResult];
[self.tableView reloadData];
It seems to do nothing and I can't see any change in the UI. I have tried to set the frame on the tableView and still no results.
Have any of you seen this behavior?
Joe
Hi Joe can you give me more code for see what are you going? what I get, is that you have your control and a tableview view inside that you want to shot just when you have your array with the data? if is like that why you don't set Hide the tableview when you haven't the data? every time that you refresh the data for the table you have also to use the method reloadData for refresh the table
Depending on the state of the data, I remove the tableview from my viewControllers view.
Consider segueing to another view controller with a table view instead of swapping out table views in a single view controller. Or, if the new data is a subset of the old data, then leave the table view in place and filter its data.
Related
I have a UITableViewController that allows for adding to it's cells through another TableviewController where the user can enter their data. I am passing the NSString data back to the first TableViewController properly as the NSLog states. However, the first TableViewController is not re-firing. I believe this is caused because I am using a modal transition. Would somebody explain to me how to make a UITableViewController re-fire with modal transitions?
in
-(void)viewWillAppear:(BOOL)animated
after adding to the table input array(datasource )
call
[tableView reloadData];
the main tableview will have rows that is added/created from the user and that row will open their own tableview which also have their own add/create own row. My main question is how to make sure each row thats added in the main tableview opens their own tableview (as blank until added from user).
This link i've seen is the closest How can I move to another view controller when the user clicks on a row? but not exactly what Im looking for. Most of the samples/tutor is all ususally populated or already have an array in their tableview. I wanted to make mines both completely nothing in it but added only by the users and main tableview each row opens its own tableview.
I already have two tableview one is main and other is sub to each row. I was able to insert row in main table view but it opens the same tableview as the other rows in the main tableview and would like to know how to make sure each row in main tableview opens their own blank tableview.
Usually your view controller controls the tableView. I guess you are trying to fit in there multiple table views and logic for each one. Instead, just create controllers for each table.
First create your main view controller using a UIViewController template (don't use a TableViewController). Use a XIB and add there a TableView element, OR create a UITableView instance using code.
Now create a class conforming to the delegate and datasource, protocols. This doesn't need to be a view controller. Example:
// MiniTVC.h
#interface MiniTVC : NSObject <UITableViewDelegate, UITableViewDataSource>
#property (nonatomic, retain) IBOutlet UITableView *tableView;
#property (nonatomic, retain) NSArray *array;
-(id) initWithTableView:(UITableView*)tableView array:(NSArray*)array;
#end
and implement the delegate and datasource methods as usual. You need at least the following:
tableView:heightForRowAtIndexPath:
numberOfSectionsInTableView:
tableView:numberOfRowsInSection:
tableView:cellForRowAtIndexPath:
Then back to your main view controller, you create (or hook from the interface designer using IBOutlets) a tableView and set its delegate and datasource to your MiniTVC class, eg:
// sample table created by code
CGRect frame = CGRectMake(50,50,100,100);
UITableView *tableView = [[UITableView alloc] initWithFrame:frame]
[self.view addSubView:tableView];
// set the datasource/delegate
MiniTVC *miniTVC = [[MiniTVC alloc] initWithTableView:self.tableView array:someArray];
tableView.dataSource = self.miniTVC;
tableView.delegate = self.miniTVC;
You can repeat this process creating another UITableView + MiniTVC from inside a custom UITableViewCell. Or you could make the UITableViewCell conform to the table delegate/datasource and implement there the methods.
Btw, creating tableviews inside tableviews is a bit unusual. Since they are both UIViewScroll subclasses, it could lead to unexpected behaviour, tho I haven't tried myself.
I'm using a UITableViewController and want to add a searchbar.
My options are [self.tableview addSubview:searchBar] or self.tableview.tableHeaderView = searchBar
Both options will scroll the searchbar along the rest of the tableview, which I understand. But is there a way to lock elements up, instead of using a UIViewController or changing the frame origin on scroll?
I'm thinking of a way to get above the current view hierarchy and add a subview onto that.
I tried the opposite approach, to add a view to the superview and bring that to front
[[self.tableView superview] addSubview:searchBar];
[[self.tableView superview] bringSubviewToFront:searchBar];
}
but it didn't work.
UITableViewControllers are UIViewControllers that have their main view property being an UITableView. Thus this UITableView do takes all the screen.
The approach you are looking for is to use a standard UIViewController whose view is a standard UIView, and add your UISearchBar and the UITableView as a subview to it in IB.
To do this, simply:
Change the class of your view controller and instead mention that it conforms to the tableview-associated protocols : change #interface UITableViewController to #interface UIViewController <UITableViewDelegate, UITableViewDataSource>
Add a #property(nonatomic, retain) IBOutlet UITableView* tableView property to your header file, and the associated #synthesize tableView; statement in the implementation. Don't forget to set this property back to nil (self.tableView = nil) in your dealloc for good memory managment
Link your UITableView instance in InterfaceBuilder to this newly created IBOutlet.
Thus you still have a UITableView but this won't be the main view that takes all your screen; instead you can layout your tableView like you want and make it take only a part of your screen, and position the UISearchBar above it.
Another approach would be to use the UISearchDisplayController to manage your UISearchBar.
I've got the following situation: I've got a UISwipeGestureRecognizer hooked up on my table cell. On swipe, two buttons get their .hidden-status switched to NO.
Those cells are custom made. Meaning, the IBAction of those buttons is in that controller. Now I'd like the UIButton in those custom cells to send back a variable to the parent view. That parent view is the UIViewController (containing a UITableView).
How can I do this? I've read about protocols, but I can't get a solid example for that... Or are there any other ideas? They are all welcome :)
The approach you are following may result in a crash or a memory leak as you have to retain the controller of your uitableview cell content.
However protocols are simple approach where after you create your table view cell you can allocate the viewcontroller (which creates the table view) as a delegate to all actions for the cell's content view.
In your custom cell's controller just call the delegate method and it will be handled by your parent view.
It may sound confusing, but its really simple. Will try to post sample code.
#protocol yourDelegate;
#interface yourViewController{
id<yourDelegate> delegate;
}
#property (nonatomic,assign) id<yourDelegate> delegate;
#end
#protocol yourDelegate
- (void)passThisActionToMainController;
- (void)passThisEventToMainController;
#end
Once you are done with the above in your table view cell view controller, in your main controller create an instance of your view controller and then
controller.delegate = self;
In your cell's view controller when a user taps or performs any action then call
[delegate passThisActionToMainController];
or
[delegate passThisEventToMainController];
This way your main controller gets the event and it can be processed.
Alternate way to do this would be to create the buttons when you create the cell and handle the events. I would recommend this approach.
[myButton addTarget:self action:#selector(whatMyButtonShouldDo:)
forControlEvents:UIControlEventTouchUpInside];
The above line is copied from an answer here.
normally, the way to update a tableView is calling self.tableView reloadData (or setNeedsDisplay), but my (visible) tableView doesn't reload by calling this method.
My context is, that I've got a Navigation-Bar and a TableView and by clicking a button on the Navigationbar, the method "aktualisieren" starts. This method also works (i use a UIAlertView to check it) But the Context just updates when i scroll the TableView (after reloading) in the unvisible area (top oder bottom). Then, just the cells which were in the outside area are reloaded. Why? Can anyone helps me?
Thank you very much,
Jonathan
It sounds like your IBOutlet might not be hooked up.
Check that you have an IBOutlet of type UITableView in your view controller and that its connected correctly to the table that you want to update in Interface Builder.
If you MyViewController.h file you should have
IBOutlet UITableView *myTableView;
#property(nonatomic, retain) UITableView *myTableView;
and in interface builder ctrl-drag from the FileOwner (MyViewController) to the table view you want to update and selected myTableView to link it.
One thing to check would be to make sure that self.tableView is hooked up to the actual table view.
I often forget to do this, and if this is the case, then the reloadData message is not reaching the table view.