Custom UITableViewController inside Container View - ios

I am trying to insert a custom UITableViewController inside a Container View. The Container View is placed inside a cell of a static UITableView as shown in the figure below.
http://i.stack.imgur.com/A762B.png
I simply want a method to combine static with dynamic cells in the same screen.
In the Identity Inspector when the field Class is empty (i.e. a standard UITableViewController) it works showing an empty dynamic table inside the cell. But when I put my custom class name (that extends UITableViewController) in that field I get a NSInternalInconsistencyException:
[UITableViewController loadView] loaded the "Enx-aT-Rum-view-zY2-9U-Z6d" nib but didn't get a UITableView.
These are the contents of MyCustomUITableViewController:
#implementation MyCustomUITableViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
#end
I have to admit that I still don't understand all the logic behind Container View, but I just want to show only one view inside (not doing any swapping or else).
Any help would be appreciated, thanks!

Let me point out the issues here first-
Problem 1: you can not have an controller inside another controller. That means, inside your Static tableview, you can not have a dynamic tableview Controller.
Solution 1:You can have a dynamic TableView.
Problem 2: You can not have a static Tableview inside a View Controller. If you want a Static Tableview then you need to have a UITableViewController rather than the UIViewController.
Solution 2: You just need to delete the ViewController you have and replace with a UITableViewController.
Now to achieve one controller where you can have a dynamic TableView inside a static tableview, you will need to implement you dynamic table's datasource inside the Static Table's ViewController which would be a very bad practise.
The static table should not need to know anything about your dynamic Table, at least about the data that will be populated in your dynamic table. However, if you want a dynamic tableview inside your static tableView then your static tableview's controller needs to implement the UITableViewDatasource.
So, you may want to re-think about the structure.

Related

Is there any way to use prototype cells in UIViewController other than UITableViewController?

I made my tableView as a subview of UIViewController , and in storyboard , i add prototype cells to this scene . but it seems that the design of cells in storyboard is not working , seems prototype cells works only for UITableViewController?
here is my storyboard design which i use a tableView as ViewController's subview , and add two prototype cells in it (pink and blue cell)
and here are my tableView codes:
#implementation SubViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:#"TableViewCell"];
[self.tableView registerClass:[ATableViewCell class] forCellReuseIdentifier:#"ATableViewCell"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
TableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"TableViewCell" forIndexPath:indexPath];
return cell;
}
else
{
ATableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"ATableViewCell" forIndexPath:indexPath];
return cell;
}
}
and when i run my code ,i expect there will be a blue and pink cell , but in fact , the tableView will only got two blank cell (seems the design in storyboard not working)
If you are designing with the Storyboard,You dont need the below lines
[self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:#"TableViewCell"];
[self.tableView registerClass:[ATableViewCell class] forCellReuseIdentifier:#"ATableViewCell"];
Instead of that you need to set identifier for cell in the storyboard as in the below image
You want to check a few things to make sure your table view is wired up and knows to look for the cells in the SubViewController class.
Make sure the UITableView has its outlet set to something you can reference in code. self.tableView isn’t automatically connected unless you are using a UITableViewController subclass.
Make sure your table view has the SubViewController class connected as it’s data source and delegate. You can do this in code, or by dragging in interface builder. Try adding this to your viewDidLoad method: self.tableView.dataSource = self;
You probably want to set up your cells to use string identifiers in the inspector panel of Interface Builder. Then, in cellForRowAtIndexPath:, dequeue your cells using that identifier, instead of registering by class name.
As an aside, you can also use prototype cells with UICollectionView but the API and storyboard setup are very similar, and the same steps apply.

Custom UITableViewController inside Container View: NSInternalInconsistencyException

I am trying to insert a custom UITableViewController inside a Container View. The Container View is placed inside a cell of a static UITableView as shown in the figure below.
I simply want a method to combine static with dynamic cells in the same screen.
In the Identity Inspector when the field Class is empty (i.e. a standard UITableViewController) it works showing an empty dynamic table inside the cell. But when I put my custom class name (that extends UITableViewController) in that field I get a NSInternalInconsistencyException:
[UITableViewController loadView] loaded the "Enx-aT-Rum-view-zY2-9U-Z6d" nib but didn't get a UITableView.
These are the contents of MyCustomUITableViewController:
#implementation MyCustomUITableViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
#end
I have to admit that I still don't understand all the logic behind Container View, but I just want to show only one view inside (not doing any swapping or else).
Any help would be appreciated, thanks!
After banging my head on the desk for so many hours I managed to resolve this.
I had to alloc/init the tableview of MyCustomUITableViewController. I override the loadView method of MyCustomUITableViewController:
- (void) loadView{
self.tableView = [[UITableView alloc]init];
}
And it is good to go!

UITableView - Accessing in Code from ViewWillAppear

I would like to manipulate table cells in my UITableView as it comes back from its detail view, to remind the user what row they last selected. The UITableView was created in IB and there currently isn't a #property or custom class for the UITableView or any other handle. Clearly, it exists when, the user is coming back from the detail view and viewWillAppear is called but I don't see any handle in the debugger for my UITableView.
Any way to simply get a handle to the IB tableView object?
// - The Containing ViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSIndexPath *selected = [self.tableView indexPathForSelectedRow];
if(selected){
//Do something stylish with the last-selected row
NSLog(#"Last row selected was: %#",selected);
}
}
I solved this by creating a class member of NSIndexPath * and set it inside didSelectRowAtIndexPath. Then, when viewWillAppear executes, when the user navigates back fram the table view detail, I have the last-selected row so I can easily highlight it or treat it any way I want:
//The Containing View Controller
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if(self.lastSelectedRow){
[self.tableView selectRowAtIndexPath:self.lastSelectedRow animated:YES scrollPosition:UITableViewScrollPositionNone];
}
}

Dynamically add images to a prototype custom cell

I have a UIViewController with an embedded tableview.
The prototype cell has been customized (and subclassed) to add a "slider" effect started by a pan gesture recognizer.
I've followed this tutorial to do it: https://github.com/spilliams/sparrowlike
Inside the cell I've got two views, a front view that slides away, and a back view that stays there showing other controls.
Now I need to add an image to every cell, this image will be chosen between two images depending on a BOOL variable.
The problem is: if I add the image view programmatically, the image will be added to the cell and then when the user swipes away the front view, the image stays there.
So the image should be added to the front view, but I can't do it in the StoryBoard and add an outlet to it.
So the question is: should I put the code to retrieve the image in the custom cell subclass?
If so, where should I put it?
My "CustomCell" class implementation file looks like this at the moment:
#import "CustomCell.h"
#implementation CustomCell
#synthesize frontView;
#synthesize backView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
What about something like this?
- (void)setupCell {
if (yourBoolValueHere) {
[[self frontView] setImage:[UIImage imageNamed:someImageName1]];
} else {
[[self frontView] setImage:[UIImage imageNamed:someImageName2]];
}
[[self view] bringSubviewToFront:[self frontView]];
}
Then, you can just call [cell setupCell] in tableView:cellForRowAtIndexPath:.

UIWebView not filling a UITableView cell

I have a webview in a uitableview cell. The problem is that the web view only takes up 75% of the uitableview cell
I am not sure how iOS decided the height of the web view, as I can't seem to increase how much space it takes. I can increase the tableview cell's height higher or lower, but the web view does not increase.
One thing I imagined I needed to do was somehow get the height of the web view itself based on how big the text is so that it can automatically scale the size of the tableview cell.
a) I'm not sure what function retrieves it
b) I think this would go in (void)webViewDidFinishLoad:(UIWebView *)webView method, but I can't get the app to go into that function. I tried debugger and it seems not to go in there at all. (web view object is within its own controller)
#import "WebViewCell.h"
NSUInteger const WEB_VIEW_ROW_HEIGHT = 75;
#implementation WebViewCell
#synthesize webView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
and in the controller which actual populates the tableview cell with the web view:
WebViewCell *cell = [[ViewManager instance] loadWebViewCell:self.tableView cellID:eventDetailsValueCellID];
NSString *htmlText = [news valueForKey:propertyName];
[cell.webView loadHTMLString:htmlText baseURL:nil];
return cell;
I'm not sure where to put the (void)webViewDidFinishLoad:(UIWebView *)webView method with this setup. Does it go in the WebViewCell.m or the controller that calls WebViewCell.m
also,the root question is how can I make the web view scale to the size of the cell?
The (void)webViewDidFinishLoad:(UIWebView *)webView needs to be implemented by the UIWebViewDelegate.
Try adding cell.webView.delegate = self in the cellForRowAtIndexPath method and also adding the delegate protocol 'UIWebViewDelegate' to your controller's header file.
This should at least get your (void)webViewDidFinishLoad:(UIWebView *)webView call to fire allowing you to do your height calculation after the web view has loaded.
The calculation would probably look something like:
webView.frame = CGRectMake(0, 0, webView.superview.frame.size.width, webView.superview.frame.size.height);

Resources