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!
Related
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.
This one is driving me crazy - I don't know what am I missing.
here is my ViewController.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.tableView registerClass:[CurrentMatchCell class] forCellReuseIdentifier:#"CurrentMatchCell"];
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(#"1");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"2");
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"3");
CurrentMatchCell *cell = (CurrentMatchCell *)[tableView dequeueReusableCellWithIdentifier:#"CurrentMatchCell"];
if (cell == nil) {
NSLog(#"XXX");
}
[cell.matchDescription setText: #"Home Team vs Away Team"];
return cell;
}
Here is screenshots from the app.
delegate and datasource are set programmatically.
cell attributes :
And the .h file :
#interface CurrentMatchesViewController : UIViewController <NSFetchedResultsControllerDelegate,UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
So, I can see logs 1,2,3 being printed out, cell is not nill but I do not see my content. Why is that?
I only see a number of empty white cells (even if I return 0 or whatever it does show the same every time).
Thanks
If you create your table view and your cell prototypes in a storyboard, the storyboard loader takes care of registering the cell prototypes that you defined in the storyboard. So:
You don't need to call registerClass:forCellReuseIdentifier: again in the code. This will actually mess up your storyboard settings.
You can also use dequeueReusableCellWithIdentifier:forIndexPath: instead of dequeueReusableCellWithIdentifier:. That method always returns a cell, so you don't have to have a nil check.
Edit: If that doesn't do the trick, try calling [self.tableView reloadData] after setting the delegate / data source, or set the delegate and data source in the storyboard.
It is because you're not loading your custom nib. Try this. (Make sure CurrentMatchCell is the name of your xib file).
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UINib *nib = [UINib nibWithNibName:#"CurrentMatchCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:#"CurrentMatchCell"];
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
Edit: based on your comment: Don't register the custom cell class when using a storyboard.... it does that for you and it also sets the delegate. So try removing those lines from the viewdidload.... and second I would try actually making CurrentMatchesViewController a subclass of UITableViewController
I have a UITableView inside of a View. I've dragged the size of the UITableView to only displays 4 rows at a time. In the ViewController code that loads data into that UITableView, I've generated 6 items to load into the UITableView, because I wanted to verify that it would scroll.
However, when I run the program and preview it in iOS Simulator, it is not scrolling. I have checked that scrolling is enabled (Screenshot).
Here is the code in which the UITableView is populated:
#import "APFacebookFriendsViewController.h"
#interface APFacebookFriendsViewController ()
#end
#implementation APFacebookFriendsViewController
#synthesize facebookFriendsTableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// set View title
self.title = #"Facebook Friends";
// load Facebook friends
self.facebookFriends = [self getFacebookFriends:#"Test Facebook User"];
}
- (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 the number of rows in the section.
return [self.facebookFriends count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"CellIdentifier";
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
UITableViewCell *cell = [self.facebookFriendsTableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
// Configure the cell
NSString *facebookFriend = [self.facebookFriends objectAtIndex:[indexPath row]];
[cell.textLabel setText:facebookFriend];
return cell;
}
- (NSMutableArray *)getFacebookFriends:(NSString *)facebookUser
{
// placeholder data
NSMutableArray *facebookFriendsArray = [NSMutableArray array];
[facebookFriendsArray addObject:#"Friend A"];
[facebookFriendsArray addObject:#"Friend B"];
[facebookFriendsArray addObject:#"Friend C"];
[facebookFriendsArray addObject:#"Friend D"];
[facebookFriendsArray addObject:#"Friend E"];
[facebookFriendsArray addObject:#"Friend F"];
// END placeholder code
return facebookFriendsArray;
}
#end
It's pretty hard to tell what you're doing wrong without looking at your whole project. A few things that you might want to try:
Make sure that the table view is on the top of the other views
Check that table view's userInteractionEnabled is set to true.
Make sure that the table view's delegate and data source points to your view controller.
Try to make the table view full size and see if it's still not scrolling. If so, probably you didn't set one of the table view properties right. You can try to remove and re add the table view.
There are many more things that can go wrong, and as I said it's pretty hard to tell without seeing your project.
1.Check Your table view, and its superviews, has userInteractionEnabled set to TRUE.
2.The custom view that the table view is inside is smaller than the table view with clipsToBounds=NO, a subview that beyond the bounds of its parentview and its not interacted with. so set to clipsToBounds YES.
Insert this into viewDidLoad;
table.delegate=self;
table.datasource=self;
Im making a project for school and I'm having trouble with table view controllers, i want to add a button to link it to another page thats really it, So i have a list of 7 table cells which connect to the detail view controller, from there i want to add a button which would link that to another page but each of the 7 table cells need to go to different pages, sorry I'm not very good at explaining things but if any one can help me it would be really appreciated.
TableViewController.m
#import "tableViewController.h"
#import "TableCell.h"
#import "DetailViewController.h"
#interface tableViewController ()
#end
#implementation tableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
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;
_Title =#[#"Centrum Advance", #"Elite Whey Protein", #"USP LABS Jack3d", #"NitroCore 24", #"PHD Synergy ISO 7", #"REFLEX One Stop", #"MET-Rx G.C.M.O"];
_Images=#[#"1C.jpg",#"2C.jpg",#"3C.jpg",#"4C.jpg",#"5C.jpg",#"6C.jpg",#"mets.jpg",];
_Review=#[#"Not Reviewed",#"'I'm a big fan of whey protein becasue the dosing is simple, easy to consume, and usually a lot cheaper than pills. I use to use the regular old GNC 100% whey protein which was good to take after a workout, but I really wanted more from my supplements. Proto Whey had a lot of great reviews and I have to say, it worked pretty well for me.'",#"'Wow. This stuff is amazing. No fillers, the taste (lemon-lime) isn't overwhelming, and I can work out solid for 1.5 hours and still feel like a beast. I've taken NO-Xplode and Black Powder, and I find that Jack3d provides more energy, while something like Black Powder gives me more pump. This is why I mix a scoop of Black Powder and a scoop of Jack3d and get totally effed at the gym.I've come to notice that if I take jack3d later in the day, it really messes with my sleep, and I contribute that to the DMAA in it, which is a stimulant. I just finished a bottle and I notice my tolerance was going up. On days that I was taking just jack3d, since I like to mix it up, I moved up to 2 scoops towards the end of the bottle. So all in all, awesome for energy in the gym, only somewhat decent regarding pump. But I still give it a solid 9. I have yet to try out 1.M.R, which I will be getting next week, and can find out then which is better for me.BTW I didn't get a 'crash' that so many people speak of, this could be that maybe I am not just prone to the type of thing, and that I work out after I get off work, so 'crashing' really doesn't matter for me.'",#"Not Reviewed",#"'I had the double chocolate flavor and it was the BEST tasting stuff I've ever had. It was so good, sometimes I just took a serviing for breakfast if I was running short on time. The effectiveness was good too, and after finishing a tub of it, I noticed just about all of my lifts increased. I gained some great vascularity in my bi's, and my chest strength exploded. Honestly, for all of you who want to spend a little money and get a nice supplement...this is your winner. I am definitely gonna use this for a while until I get sick of the taste or see a new up-and-coming product. Only reason I give value a 5 is because I paid about $40 for the 2LB tub back in January.'",#"Not Reviewed",#"Not Reviewed",];
_Size=#[#"1.2KG",#"3KG",#"2.7KG",#"3.9KG",#"5KG",#"6.4KG",#"0.5KG",];
_Does=#[#"The perfect whey protein to aid weight loss or pack on lean muscle mass",#"Add some serious muscle to your body without the fat!",#"24g of protein per scoop for just 112 calories and no sugars!",#"Premium Whey Isolate",#"Minimal carbohydrates and fats",#"The Only Way To Enjoy Whey Protein",#"27g of protein with each serving",];
}
- (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 the number of rows in the section.
return _Title.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
int row = [indexPath row];
cell.TitleLabel.text = _Title[row];
cell.ThumbImage.image = [UIImage imageNamed:_Images[row]];
cell.Review1.text = _Review[row];
cell.Size1.text = _Size[row];
cell.Does1.text = _Does[row];
return cell;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:#"ShowDetails"]) {
DetailViewController *detailviewcontroller = [segue destinationViewController];
NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
int row = [myIndexPath row];
detailviewcontroller.DetailModal = #[_Title[row],_Images[row]];
}
}
#end
DetailViewController.m
#import "DetailViewController.h"
#interface DetailViewController ()
#end
#implementation DetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_TitleLabel.text = _DetailModal[0];
_ThumbImage.image = [UIImage imageNamed:_DetailModal [1]];
self.navigationItem.title = _DetailModal[0];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
the table cell.m is empty and the .h is used for the IBOutlets
Create a public #property on the DetailViewController that you can set in prepareForSegue:sender: TableViewController to setup the button's target in each individual DetailViewController.
// DetailViewController.h
#interface DetailViewController
#property NSInteger nextPageId; // or whatever
// TableViewController.m
// prepareForSegue...
detailviewcontroller.nextPageId = myIndexPath.row; // something like this
// DetailViewController.m
// viewDidLoad...
[myButton addTarget:self action:#selector(myButtonHandlerMethod) forControlEvents:UIControlEventTouchUpInside];
-(void)myButtonHandlerMethod {
// code to present the next "page" based on self.nextPageId
}
The method addTarget:action:forControlEvents: sets up another method myButtonHandlerMethod to be called when the user taps the button. If the UIButton is in the DetailViewController Storyboard then create a IBOutlet property and reference the button as self.myButton.
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:.