I've got a problem with my cells in my table view controller.
I've created a table view controller with some cells that the user is able to edit but this cells doesn't display...
Here my code for the view controller :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
return cell;
}
And the header :
#interface AircraftDetailsViewController : UITableViewController
#property (weak, nonatomic) IBOutlet UITextField *AircraftNameLabel;
#property (weak, nonatomic) IBOutlet UITextField *AircraftImmatLabel;
If someone could help me... I don't understand why it is empty...
I've forgotten to add that my cells are "Static cells".
EDIT : Problem sovlved. I just comment these line :
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//
// // Return the number of sections.
// return 0;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//
// // Return the number of rows in the section.
// return 0;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
// static NSString *CellIdentifier = #"Cell";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//
// // Configure the cell...
//
// return cell;
//}
Now it works.
If you have static tableviews in the storyboard, don't implement any datasource methods. UITableViewController has its own implementation of these which you have overridden in your code above. If you do override the methods, you must call the super implementation, to get the value of your static cells, and then modify as you see fit.
Even if you weren't using static cells, you still wouldn't see anything, because you've told your table view that there are no sections and no rows in the table.
For starters you are returning 0 sections and 0 rows so nothing will be displayed. You should return values > 0 according to your data layout.
After you fix that make sure that "Cell" identifier is defined in IB (In the Cell's properties pane although I believe it's the default name and should be there already).
In your code you specify table have no cells at all.
Return other then 0 value in number* functions
You forgot to declare protocols in .h file. Change this #interface AircraftDetailsViewController : UITableViewController to #interface AircraftDetailsViewController : UITableViewController<UITableViewDataSource, UITableViewDelegate> And you are returning 0 in datasource method which is wrong.
The sections and rows must be a non zero value
- (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 10;
}
So delete above code and set controller as table data source and table delegate in interface builder with ctrl+drag from tableview to controller. For static cells it's enough.
You told number section and row as zero to tableview via delegate methods. So It shows nothing. According to this, your cellForRowAtIndexPath: never getting called.
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 have a tableViewController, and under that i want one static cell and rest of all will be dynamic cells . I have already run for dynamic cells , but within same tableViewController i also need to add one static cell, how can i achieve it?
Please Help
Thanks in advance.
you could do something like the following:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dynamicContent.count + 1; // +1 for the static cell at the beginning
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
// static cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"StaticCellIdentifier" forIndexPath:indexPath];
// customization
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"DynamicCellIdentifier" forIndexPath:indexPath];
id contentObject = self.dynamicContent[indexPath.row];
// customization
return cell;
}
You cannot create static and dynamic cell at same time in a UITableViewController.
But you can hard code your static cell's data and load the data each time you reload your tableview.
You can keep all your cells in one section and keep checking for index path.row == 0 or create separate sections for them.
typedef NS_ENUM(NSUInteger, TableViewSectionType) {
TableViewSectionType_Static,
TableViewSectionType_Dynamic
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2; // One for static cell, and another for dynamic cells
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
switch(section) {
case TableViewSectionType_Static:
return 1; // Always return '1' to show the static cell at all times.
case TableViewSectionType_Dynamic:
return [myDynamicData count];
}
}
With this approach your cells will be split into two sections and it will be easier to manage. And it will always show one cell, as number of rows returned for TableViewSectionType_Static is 1 always. It will show the dynamic cells based on your data count.
This question already has an answer here:
How to make a Sub Table view in a TableViewCell
(1 answer)
Closed 9 years ago.
In one of the cell of a tableView, i want to put another tableView(toyTable for example). Can anyone help me in this? I have created a tableView (main one) and another tableview in my project. Now in my main tableView, i have many cells and in one of the cell, i want that another table(toyTable) to be present in the cell.
Lets say you have two tables.
Make properties for both of them
// This one might be connected as an outlet with your storyboard
#property (nonatomic) IBOutlet UITableViewController *mainTable;
// This one should be in the .m file
#property (nonatomic) UITableViewController *toyTable;
Implement delegate and datasource protocol methods for each.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.mainTable) {
return 2;
} else {
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.mainTable) {
return 10;
} else {
return 5;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.mainTable) {
// If this is the indexPath for the cell that should contain
// the nested table view, initialize your self.toyTable, set
// datasource and delegate etc.
// Else configure your default outer cell
} else {
// Configure the cells of your self.toyTable;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.mainTable) {
// If this is any normal outer cell
return UITableViewAutomaticDimension;
// If this is the outer cell that contains self.toyTable
return some bigger value (if desired)
} else {
// The height of the inner cell
return UITableViewAutomaticDimension;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.mainTable) {
// If this is the outer cell that contains self.toyTable
// You should ignore the selection
// Else handle it according to your needs
} else {
// Handle click on inner cell
}
}
If you are using a static table view as your mainTable, you can directly drag a table view onto one of your static cells and define prototype cells etc for it.
Putting table view inside table cell is a bad design. Insert toyTable as a section of main table view instead.
In cellForRowAtIndexPath add that table and do the code like this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == mainTable) {
//do this for main table
}
else {
//do this for second table
}
}
Why are u doing this because its a bad idea for nesting the tableview instead UITableview already having expand and collapsing property.
Use below link for reference http://www.cocoanetics.com/2011/03/expandingcollapsing-tableview-sections/
I have an application (Navigation Controller) in which the second controller is a Table View Controller. I am able to send my required data through the segue, but unable to update the entries in the tableview programmatically.
I'm new to IOS.
This is my header file
#import <UIKit/UIKit.h>
#interface HCIResultListViewController : UITableViewController
#property (strong,nonatomic) NSMutableDictionary *resultList;
#property (strong, nonatomic) IBOutlet UITableView *tableListView;
#end
This is my implementation file.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"HelloCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Hello";
// Configure the cell...
return cell;
}
Can anyone tell me how to work with tableview & Controllers?
I did go through several documentation and examples but really couldnt understand
You need to specify the rows and section count for the table view. like this.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [sourceData Count]//i.e., 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"HelloCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Hello";
// Configure the cell...
return cell;
}
What would you expect in your UITableView if you set the numberOfRowsInSection to 0?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 0; // it should be size of your Array or List
}
You should change the number of rows to the Array size or whatever data you are populating within the UITableView
you need to return at least 1 to be able to see some data
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Number of sections of tableView is 0! make it 1
It may help you
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 4;
}
replace your method this :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
return 1;
}
That ist exactly what Apple's template meant by stating:
#warning Potentially incomplete method implementation.
You need to return appropriate values there.
Return 1 from numberOfSectionsInTableView if you just have 1 section (that ist the normal case I would say).
And return the number of rows that you want to be filled from numberOfRowsInSection. That may well be the count of an array or the number of fetched data sets or some hard coded value/constant value if it is not dynamic.
cellForRowAtIndexPath will then be called as maximum for each row/section combination that is possible but only for those cells that are visible from start or are about so be scrolled into visible areas.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//warning Incomplete method implementation.
// Return the number of rows in the section.
//
return 1;
}
As per this Datasource Method say that as number row require and you have to return 0 so not generate any row so also not generate cell so as per our requirement static otherwise dynamic generate the row at least 1 row return.
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
}