This question already has answers here:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is not being called
(4 answers)
Closed 9 years ago.
I know the title is illustrating enough, and I KNOW that this question has been ask a couple of times, but in this case I couldn't manage to make it work.
I already have worked with UITableViews and they all have been working fine, but this time, I even checked my code with other working copies, but it is not working.
So, here's the story :
I have a table view in one of my view controller's in storyboard with a custom class named ContactViewController, and it is connected to code using an outlet named favContactsTable
Here are related parts from its .h and .m :
ContactViewController.h
#interface ContactViewController : UIViewController <UITableViewDataSource>
// Other Stuff
.
.
#property (retain, nonatomic) IBOutlet UITableView *favContactsTable;
.
.
// Other Stuff
#end
And here is implemented functions of UITableViewDataSource protocol :
ContactViewController.m
// Rest of the code
.
.
.
.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"TEST OUTPUT");
//Test
int a = 1; // Breakpoint
a++;
cell = [self.favContactsTable dequeueReusableCellWithIdentifier:#"FavContactCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"FavContactCell"];
}
cell.textLabel.text = #"FAV";
.
.
.
}
I have another button in my view controller that reloads data for this table, but when I press it none of the breakpoints is triggered and no log is printed, none of the functions are being called. Here's the code for that button :
- (IBAction)refreshTables:(id)sender
{
[self.favContactsTable reloadData];
}
I checked almost every part with my other projects that has a dynamic table view like this, and I can't figure out what's wrong.
Please do not mention other similar questions as duplicates, because I already have read them all ( or at least, most of them ) and I'm sure I'm not returning 0 rows for each section, and I'm sure favContactsTable is not nil when I send reloadData message to it and ... etc !
Any idea that may help is really appreciated.
#interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
}
set delegate
drag to your viewcontroller icon
check your cell identifier as same in your code also
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SimpleTableCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:#"SimpleTableCell"];
}
return cell;
}
try this code :
#interface ContactViewController : UIViewController <UITableViewDataSource, UITableViewDelegate >
.
.
.
and then in viewDidLoad add delegate to your tableview property example :
self.favContactsTable.delegate = self;
You did not confirmed these protocols :UITableViewDataSource, UITableViewDelegate.Add this to your file.
and after that:
favContactsTable.delegate=self;
favContactsTable.datasource=self;
and attach directly from your Xib file.
Related
I want to put a tableview into a tableview cell. I like the formatted look it gives. I'm hoping to put in a few fields, for like name, email, etc. What am I missing to be able to make this work? Currently I can not set "ProfileCell" as the tableview class.
In my .h file of the profile cell I added:
#interface ProfileCell : UITableViewCell <UITableViewDataSource,UITableViewDelegate>
and in my .m file I added some basic methods for the tableview:
#import "ProfileCell.h"
#implementation ProfileCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Number of rows is the number of time zones in the region for the specified section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Doing this");
static NSString *MyIdentifier = #"MyReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
return cell;
}
#end
EDIT:
I have seperated the code for the cellview tableview into it's own files, and linked tableview to this class, however it does not fire when viewed:
#interface ProfileBasicDetails : UITableView <UITableViewDataSource,UITableViewDelegate>
Per comments below
This is what my page should look like when said and done. It's so users can enter in their details, while looking nice and formatted, but not ghetto looking.
I'm assuming you've embedded the UITableView into the cell?
You should wire the tableview to an outlet in the cell. Then set the delegate on the tableview in your awakeFromNib method.
*As vikingosegundo mentioned in the comments, this is potentially a pretty fragile approach.
I'm facing a strange problem. I have an iOS 7.1 storyboard app with a UITableViewController using Xcode 5. I created a subclass of UITableViewCell and set it as the class for the cell from the IB and also added a cell identifier. The style is set to Custom as well. I haven't made any modifications to this subclass yet though. It has only the template code you get when you create a new class.
In the table view controller I have this code. Just setting the data source and the delegate methods.
#import "TableViewController.h"
#import "TextFieldTableViewCell.h"
#interface TableViewController ()
#end
#implementation TableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
TextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = #"hello world";
return cell;
}
In the cellForRowAtIndexPath, I simply set a value to the textLabel property of the cell. Even that doesn't get shown up. I double checked the identifier, style and everything bit they are all set.
I have no idea why this isn't working. I also checked out a few online tutorials on this subject matter like this one but I have everything as they've described.
Can anyone please tell me if I'm missing anything?
I've uploaded a test project to my Dropbox here if you want to take a quick look at it.
Found it! You've overridden the layoutSubviews of TextFieldTableViewCell class, and wrote nothing inside of it.
Just remove the layoutSubviews (for now) and it will work.
Update: A better solution;
- (void) layoutSubviews
{
[super layoutSubviews];
}
PRESENTATION
Mine is a simple project: It consists of a NavigationController, ViewController, and a “Search Bar and Search Display Controller”
My .h file is
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate>
#end
and my .m file is
#import "ViewController.h"
#interface ViewController ()
#property(nonatomic,strong)NSMutableArray *data;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
self.data=[[NSMutableArray alloc]init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [_data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Configure the cell.
cell.textLabel.text = [NSString stringWithFormat:#"Row %d", indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"Row %d: %#", indexPath.row, [_data objectAtIndex:indexPath.row]];
return cell;
}
#pragma mark - delegate
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(mockSearch:) userInfo:searchString repeats:NO];
return NO;
}
- (void)mockSearch:(NSTimer*)timer
{
[_data removeAllObjects];
int count = 1 + random() % 20;
for (int i = 0; i < count; i++) {
[_data addObject:timer.userInfo];
}
[self.searchDisplayController.searchResultsTableView reloadData];
}
#end
And that’s the entire program. What does it do? User makes a search and the data is displayed in a TableView (similar to a google search).
PROBLEM
I need to use a CustomTableViewCell for my table. And I need to build the TableViewCell from the storyboard (easy to visualize). I am stuck with the storyboard part. How do I place a TableViewCell on the storyboard without a TableView to place it in? I had an idea, I tried it, but it didn’t work. Here is what I did. I placed a “never-to-be-used” TableViewController in the storyboard whose sole purpose is to hold my CustomTableViewCell. Then in code I subclass TableViewCell and use IBOutlet to link the sub-views of the storyboard TableViewCell to my CustomTableViewCell . And then I used my cell as CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];. This didn’t work. The tableView remained blank. And my guess for the failure is that CustomTableViewCell does not belong to the tableView being dequeued from.
I need the UISearchBar to always stay inside the NavigationBar. But so far self.searchDisplayController.displaysSearchBarInNavigationBar = YES; is not doing it. When I first start the app the searchBar is inside the NavigationBar. But as soon as I click on it, it smacks itself right in the middle of my scene/screen and never leaves.
I need to add a Header to my TableView. Which I thought of doing the same way as the CustomTableViewCell, but with a UIView parent class. But again, the CustomTableViewCell portion failed.
PLEA
Thank you for any help you can provide me.
UPDATE
All I am trying to do is allow my users to launch a server side search and view the results in a tableView. This is such a basic thing, I image many people here have done this a number of times. Being new to iOS, I am stuck. But at this point, I have posted my entire project (anyone can reconstruct it), and I have explained in details all the ways I tried to solve the problem. So if someone has a sample project they don’t mind sharing, it would help very much. Or if you know of a git project please put a link to it. Thanks.
If you want to make a stand-alone view (not in a view controller) in IB, then you should do it in a xib file, not a storyboard. You can have as many storyboard and xib files in an app as you want; they can be mixed freely. To make a new cell in a xib file, just go to New File --> User Interface --> Empty then drag in a UITableViewCell. Add any subviews you want, and in your table view controller (or whatever class is your table view data source), register the xib file with, registerNib:forIdentifier: (usually, you do this in viewDidLoad).
In my iPad app storyboard design, there are two table views added.
One table view is for displaying the folder names and the other table view is for displaying the file names. When ever a folder table view cell is selected the files inside the selected folder needs to to be displayed in the other table view(the files table view).
My problem is
I am confused about
How to add delegate and data source for each table view in ViewController ? or is it possible to add data source and delegates for each table view in a custom class other than ViewController ?
and
How to handle the selection of cell ?
Please help !
First off: Why don't you just display the files on a pushed viewController which takes up the whole screen? Seems more intuitive for me.
If you want to do it with two tableViews, and assuming they use dynamic cells than:
1, In the .h file of your view controller
specify two tableView properties like so:
#property (weak, nonatomic) IBOutlet UITableView *foldersTableView;
#property (weak, nonatomic) IBOutlet UITableView *filesTableView;
implement the two delegate protocols of the UITableVIew
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
2, Add the two UITableViews onto your ViewController, and...
...link your Outlets to the two tableviews
...on the attributes inspector set the Tag of the foldersTableView to 1 and the filesTableView to 2
...select each of the UITableViews, go to the Connections Inspector and link their two delegate methods (delegate and datasource) to your ViewController (for both of them)
3, In the .m file of your ViewController implement the three datasource methods of UITableView like so:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (tableView.tag == 1) {
return theNumberOfSectionsYouWantForTheFolderTableView;
} else {
return theNumberOfSectionsYouWantForTheFilesTableView;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView.tag == 1) {
return [foldersArray count];
} else {
return [filesArray count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.tag == 1) {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
return cell;
} else {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
return cell;
}
}
4, Implement the selection:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.tag == 1) {
//fill filesArray with the objects you want to display in the filesTableView...
[filesTableView reloaddata];
} else {
//do something with the selected file
}
}
Hope I got everything correctly. Don't forget to #synthesize the properties in the .m file if you are using pre XCode 4.4.
If you are using multiple tables, don't forget to hide as appropriate:
- (void)viewWillAppear:(BOOL)animated
{
[self.table2 setHidden:YES];
[self.table1 setHidden:NO];
// Probably reverse these in didSelectRow
}
I'm new to iPhone dev and wanted to get advice on the general design pattern / guide for putting a certain kind of app together.
I'm trying to build a TabBar type application. One of the tabs needs to display a TableView and selecting a cell from within the table view will do something else - maybe show another table view or a web page. I need a Navigation Bar to be able to take me back from the table view/web page.
The approach I've taken so far is to:
Create an app based around UITabBarController as the rootcontroller
i.e.
#interface MyAppDelegate : NSObject <UIApplicationDelegate>
{
IBOutlet UIWindow *window;
IBOutlet UITabBarController *rootController;
}
Create a load of UIViewController derived classes and associated NIBs and wire everything up in IB so when I run the app I get the basic tabs working.
I then take the UIViewController derived class and modify it to the following:
#interface MyViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
{
}
and I add the delegate methods to the implementation of MyViewController
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.row == 0)
{
cell.textLabel.text = #"Mummy";
}
else
{
cell.textLabel.text = #"Daddy";
}
return cell;
}
Go back to IB , open MyViewController.xib and drop a UITableView onto it. Set the Files Owner to be MyViewController and then set the delegate and datasource of the UITableView to be MyViewController.
If I run the app now, I get the table view appearing with mummy and daddy working nicely. So far so good.
The question is how do I go about incorporating a Navigation Bar into my current code for when I implement:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath()
{
// get row selected
NSUInteger row = [indexPath row];
if (row == 0)
{
// Show another table
}
else if (row == 1)
{
// Show a web view
}
}
Do I drop a NavigationBar UI control onto MyControllerView.xib ? Should I create it programmatically? Should I be using a UINavigationController somewhere ? I've tried dropping a NavigationBar onto my MyControllerView.xib in IB but its not shown when I run the app, only the TableView is displayed.
http://miketeo.net/wp/index.php/2008/08/31/simple-iphone-tutorial-part-3.html
this code was very helpfull for me. You can download and use this code..