I have read about using underscores to fix compiler warnings that say "Local declaration hides instance variable," but I have not been able to implement a fix. The solutions I have read about say to use an underscore in my .h file, and the #synthesize part of in my .m file. However, I do not synthesize my tableView. Please see my header file below:
.h File
#interface ListViewController : GAITrackedViewController <UISearchDisplayDelegate,
UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate> {
IBOutlet UITableView *tableView;
}
.m File
//SYNTHESIZE
#synthesize listItems, filteredListItems, savedSearchTerm, savedScopeButtonIndex,
searchWasActive, mapView, loadingImageView, loadingActivity;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//FIRST WARNING for tableView HERE
if (tableView == self.searchDisplayController.searchResultsTableView){
return [self.filteredListItems count];
}
else{
return [self.listItems count];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//SECOND WARNING for tableView HERE
[tableView setRowHeight: 60.00];
return 1;
}
You have an outlet/property named tableView and your delegate method also contains tableView.
So you need to change either of them. Typically you go to change the argument names of delegate and datasource as
- (NSInteger)tableView:(UITableView *)aTableView
numberOfRowsInSection:(NSInteger)section {
Change :
IBOutlet UITableView *tableView;
To :
IBOutlet UITableView *tableView1;
Make the same required changes in your .m file also. This warning is coming because in below delegate method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
There is a reference of UITableView with name tableView and you have given the same name. Scope of this variable is function level where as scope of your variable declare in .h is class level.
Dont use same name : IBOutlet UITableView *tableView; use tableview or table_View
I do also spent lot of time on this issue. Finally I could solve this problem.
The thing is we should take care of outlets which are created by us. If you change outlets names then you must connect them properly after change.In my case after lot of struggle I could finish this issue hope it will help others....
For example if You have an outlet/property named tableView and your delegate method also contains tableView.
So you need to change either of them. Typically you go to change the argument names of delegate and datasource as
Change :
IBOutlet UITableView *tableView;
To :
IBOutlet UITableView *tableView1;
For more information visit this link->Local Declaration Hides Instance Variable - tableView Not Synthesized
Related
I created master details template project in xcode 4.6 and I added custom cell with 2 textfields. I also created new class which is subclass of UITableViewCell and inside this class I created outlets for text fields. When user types something NSMutableArray is updated and this works fine. Now I am wondering how to pass this array back to MasterViewController (UITableViewController) so that I can use this data to show calculations.
I tried using tutorials for delegates between UIViewControllers but I keep getting errors. Any help is appreciated.
You shouldn't keep data inside the UITableViewCell, as it breaks the MVC.
You need to get a reference of the UITextField on your cell. This is how I do in a login form:
I have a custom cell subclass called TextFieldCell, it has an outlet called textField, I want that my UITableViewController have references to these UITextFields.
First I open my storyboard, set the cell class to TextFieldCell and than connect the UITextField to cell textField outlet. Than I add this to the tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
(…)
if (indexPath.row == 0) {
// Sets the textField of the first cell as the loginTextField.
self.loginTextField = tCell.textField;
} else {
// Sets the textField of the second cell as the passwordTextField.
self.passwordTextField = tCell.textField;
}
tCell.textField.delegate = self;
(…)
}
Now I can access the value of my loginTextField and my passwordTextField. I do that on the tableView:cellForRowAtIndexPath: because that's when I'm creating the cell to add to the table view.
In your case you need to create Protocol:
I just Give Basic Idea for how to Create Protocol
Also Read This Question
#DetailViewController.h
#import <UIKit/UIKit.h>
#protocol MasterDelegate <NSObject>
-(void) getButtonTitile:(NSString *)btnTitle;
#end
#interface DetailViewController : MasterViewController
#property (nonatomic, assign) id<MasterDelegate> customDelegate;
#DetailViewController.m
if([self.customDelegate respondsToSelector:#selector(getButtonTitile:)])
{
[self.customDelegate getButtonTitile:button.currentTitle];
}
#MasterViewController.m
create obj of DetailViewController
DetailViewController *obj = [[DetailViewController alloc] init];
obj.customDelegate = self;
[self.navigationController pushViewController:reportTypeVC animated:YES];
and add delegate method in MasterViewController.m for get button title.
#pragma mark -
#pragma mark - Custom Delegate Method
-(void) getButtonTitile:(NSString *)btnTitle;
{
NSLog(#"%#", btnTitle);
}
I've done this before but I have no idea what I'm missing.
I started the project as a single View project.
Then converted the ViewController to inherit from UITableViewController like so:
#interface ViewController : UITableViewController
went into the .xib for this ViewController and changed the class in the Custom Class section form UIView to UITableView:
Looking in my other project(s) where the ViewControllers are just straight TableView controllers, I don't see what else needs to be done, but when I run the app or when I view the xib it's not showing a tableview.
You'll need to drag out a UITableView in Interface Builder to replace the UIView. Then add the UITableViewDataSource and UITableViewDelegate protocols to your view controllers header file and connect the datasource and delegate outlets from your UITableView to your view controller in Interface Builder.
#interface ViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
add delegate & dataSource. i think you are forgotten to add delegates &dataSource thats why its not showing table. add following lines to your viewDidLoad().
self.myTableView.dataSource = self;
self.myTableView.delegate = self;
after this also implements the required methods of delegate, that are
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return
10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//your code for cell data
}
What init method are you using in the .m file. You need to use the
[[UITableViewController alloc] initWithStyle:UITableViewStylePlain]
allocation method and make sure you have included the<UITableViewDataSource, UITableViewDelegate> and the relevant datasource and delegate methods.
Ok,
basically, if you want to just have a UITableViewController from scratch without having to manually do the above, just create a new Obj-C class, have it inherit from UITableViewController, and select to create a xib, and it will give you a viewcontroller and view already wired to working with a UITableView:
I am trying to write an app with a 'main page' of UIButtons that when tapped will Segue to UITableView, which will in turn Segue to another View etc. I have no errors or warnings in my code, and I thought I designed it well, but when I run the app the PrototypeCells don't even appear.
This is how I have my Storyboard set up right now (I'm sorry but I can't post images yet, as I'm a new user). The little beer mug is a UIButton that successfully performs the Segue to the UITableView window (this is the one that doesn't load the PrototypeCells).
The Brew View Controller has no code in it other than the defaulted methods.
I created a class called Brewery and gave it the following properties
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy) NSString *address;
#property (nonatomic, copy) NSString *info;
#property (nonatomic, copy) NSString *brews;
I created the Brewery View Controller and added the following methods along with the default code
- (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.breweries count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"BreweryCell"];
Brewery *brewObject = [self.breweries objectAtIndex:indexPath.row]; //might be an error in here
cell.textLabel.text = brewObject.name;
return cell;
}
Yet when I run this, the PrototypeCells aren't even showing. The ReuseIdentifier matches both in storyboard and in the code, and the View is attached to the dataSource and the Delegate. Can anyone help me figure out what I'm doing wrong? I am also new to site, so please tell me if there is something I am doing wrong or if you need more information to assist me.
Even though you have said you have set the dataSource, if numberOfSectionsInTableView is not being called then it means the data source is not set correctly.
I suggest checking this in viewDidAppear and logging the data source and delegate, and programatically checking the equal self.
There are a few pitfalls with setting IBOutlets in interface builder.
From what you have said in your blurb:
the View is attached to the dataSource and the Delegate
This is wrong. It should be the file's owner which is the View Controller.
I have a searchDisplayController that searches a UITableView.
After entering the search terms, I can see another UITableView that contains the search results. However, I want this UITableView to be GROUPED, not PLAIN (like it is by default).
How do I do this?
This worked for me (iOS 5.0):
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
[self.searchController setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
forKey:#"_searchResultsTableViewStyle"];
If - like me - you think the plain TableView was way too ugly, you can also abandon the use of SearchDisplayController.
I just:
inserted in an empty View a searchBar and a TableView as we usually do for IBOutlet
selected the File's owner as delegate for both of them.
At the beginin the number of section is 0 ([myTab count]) then I used reloadData and this time myTab is populated by the result.
[self.resultTableView reloadData];
Here you can find all the method I used from the delegates
#interface MyViewController : UIViewController <UIApplicationDelegate, UISearchBarDelegate> {
IBOutlet UISearchBar *searchBar;
IBOutlet UITableView *resultTableView;
}
#property (nonatomic, retain) IBOutlet UISearchBar *searchBar;
#property (nonatomic, retain) IBOutlet UITableView *resultTableView;
//For your searchBar the 2 most important methods are
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBarClicked;
- (BOOL)searchBarTextDidEndEditing;
//For your TableView the most important methods are in my case:
//number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
//HEADERS
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
//different numbers of row by section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
//the cells themselves
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
#end
After all, the simplest solution is often the best...
This works for me:
Create a class which extends UISearchDisplayController:
#interface RVSearchDisplayController : UISearchDisplayController
#end
#implementation RVSearchDisplayController
-(UITableView *) searchResultsTableView {
[self setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
forKey:#"_searchResultsTableViewStyle"];
return [super searchResultsTableView];
}
#end
Then add a UISearchDisplayController to your table using IB, and change its Custom Class to RVSearchDisplayController in Identity Inspector.
You could try to create a subclass of UISearchDisplayController and make searchResultsTableView searchable
in any .h file add:
#interface YourUISearchDisplayController : UISearchDisplayController {
UITableView * searchResultsTableView;
}
#property (nonatomic) UITableView * searchResultsTableView;
#end;
Then just use YourUISearchDisplayController instead od UISearchDisplayController.
Note: you might have to use (nonatomic, retain), (nonatomic, assign), or (nonatomic, copy). I'm not really sure
This is not possible as the searchResultsTableView property is readonly.
Overriding -searchResultsTableView won't work, because UISearchDisplayController accesses its table view instance variable directly, without calling the method.
The designated initializer for UISearchDisplayController appears to be a private method, -initWithSearchBar:contentsController:searchResultsTableViewStyle:, which sets the _searchResultsTableViewStyle instance variable. This instance variable is used in creating the search results table view. The public initializer calls this method, passing UITableViewStylePlain.
Directly calling the private designated initializer or setting the instance variable would likely get an application rejected from the App Store, so you might instead try overriding the public initializer and calling
[self setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
forKey:#"searchResultsTableViewStyle"];
What are the most common reasons for an outlet (a class property) not being set during a bundle load?
I'm sorry; most likely I'm not using the correct terms. It's my first steps with iPhone OS development and Objective-C, so please bear with me. Here is more details. Basically, I'm trying to create a table view based form with a fixed number of static rows. I followed this example:
http://developer.apple.com/iphone/library/documentation/userexperience/conceptual/TableView_iPhone/TableViewCells/TableViewCells.html
Scroll down to The Technique for Static Row Content please. I have one nib file with one table view, three table cells and all connections set as in the example. The problem is that the corresponding cell properties in my controller are never initialised. I get an exception in cellForRowAtIndexPath complaining that the returned cell is nil: UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath.
Here are the relevant parts from the implementation of the controller:
#synthesize cellA;
#synthesize cellB;
#synthesize cellC;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.row)
{
case 0: return cellA; break;
case 1: return cellB; break;
case 2: return cellC; break;
default: return nil;
}
}
And here is the interface part:
#interface AssociatePhoneViewController : UITableViewController
{
UITableViewCell *cellA;
UITableViewCell *cellB;
UITableViewCell *cellB;
}
#property (nonatomic, retain) IBOutlet UITableViewCell *cellA;
#property (nonatomic, retain) IBOutlet UITableViewCell *cellB;
#property (nonatomic, retain) IBOutlet UITableViewCell *cellC;
#end
This must be possibly one of the the most embarrassing questions on StackOverflow. It looks like the most basic example code.
Is is possible that the cells are not instantiated with the nib file? I have them on the same level before the tabula view in the nib file. I tried to move them after the table view, but it did not make any difference.
Are table cells in some way special? Do I need to set some flag or some property on them in the nib file? I was under the impression that all classes (views, windows, controllers …) listed in a nib file are simply instantiated (and linked using the provided connections).
Could it possibly be some memory issue? The cell properties in my controller are not defined in any special way.
I found out what the problem was already some time ago, and I should have posted it earlier ... sorry. Here it goes.
The culprit was the Interface Builder - or perhaps me not using it correctly. I drag-dropped my view controller into a different nib file, and somehow managed to delete (or not fill) the NIB Name field. Either way, the view controller was instantiated without its NIB file.
try instantiating (alloc init) the cells in viewDidLoad