table view does not display data,iphone - iphone

I am working with UITableView now and what I an having is
MyClass.h
#interface MyClass : UIViewController {
}
#property (strong , strong) UILabel *name;
#property (strong , strong) UILabel *add;
and
MyClass.m
-(NSInteger)tableView:(UITableView *)atableView numberOfRowsInSection:(NSInteger)section{
return 3 ;
}
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cell";
tableCell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
...................................................
return tableCell;
}
When I put i break point at numberOfRowsInSection, it seems that it is not being executed at all.
It is confusing me now.
Please advice if you have encountered it before.
Thanks

The most common cause of this is not setting your view controller as the table view delegate and datasource. If it's built in storyboard, you can do this by selecting the connections inspector on the table and dragging those properties to the view controller.
Otherwise, set it in code like this:
self.tableView.delegate = self;
self.tableView.datasource = self;

Related

Set UITableView Delegate and DataSource

This is my problem:
I have this small UITableView in my storyboard:
And this is my code:
SmallTableViewController.h
#import <UIKit/UIKit.h>
#import "SmallTable.h"
#interface SmallViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITableView *myTable;
#end
SmallTableViewController.m
#import "SmallViewController.h"
#interface SmallViewController ()
#end
#implementation SmallViewController
#synthesize myTable = _myTable;
- (void)viewDidLoad
{
SmallTable *myTableDelegate = [[SmallTable alloc] init];
[super viewDidLoad];
[self.myTable setDelegate:myTableDelegate];
[self.myTable setDataSource:myTableDelegate];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
Now as you can see, I want to set an instance called myTableDelegate as Delegate and DataSource of myTable.
This is the Source of SmallTable class.
SmallTable.h
#import <Foundation/Foundation.h>
#interface SmallTable : NSObject <UITableViewDelegate , UITableViewDataSource>
#end
SmallTable.m
#implementation SmallTable
- (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 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text = #"Hello there!";
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Row pressed!!");
}
#end
I implemented all the UITableViewDelegate and UITableViewDataSource method that the app need. Why it just crash before the view appear??
Thanks!!
rickster is right. But I guess you need to use a strong qualifier for your property since at the end of your viewDidLoad method the object will be deallocated anyway.
#property (strong,nonatomic) SmallTable *delegate;
// inside viewDidload
[super viewDidLoad];
self.delegate = [[SmallTable alloc] init];
[self.myTable setDelegate:myTableDelegate];
[self.myTable setDataSource:myTableDelegate];
But is there any reason to use a separated object (data source and delegate) for your table? Why don't you set SmallViewController as both the source and the delegate for your table?
In addition you are not creating the cell in the correct way. These lines do nothing:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text = #"Hello there!";
dequeueReusableCellWithIdentifier simply retrieves from the table "cache" a cell that has already created and that can be reused (this to avoid memory consumption) but you haven't created any.
Where are you doing alloc-init? Do this instead:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell) {
cell = // alloc-init here
}
// Configure the cell...
cell.textLabel.text = #"Hello there!";
Furthermore say to numberOfSectionsInTableView to return 1 instead of 0:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
Presumably you're using ARC? Your myTableDelegate is only referenced in a local variable in viewDidLoad -- once that method ends, it's deallocated. (In the delegate/datasource pattern, objects do not own their delegates, so the table view's references back to your object are weak.) I wouldn't expect that alone to cause a crash, but it's likely key to your problem.
setDelegate will not retain the delegate.
And
numberOfSectionsInTableView method has to return 1 instead of 0;
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 0;
}
Number of sections should be set at least one
The delegate of an UITableView object must adopt the UITableViewDelegate protocol. Optional methods of the protocol allow the delegate to manage selections, configure section headings and footers, help to delete methods.


UITabBarController with UITableView

i am trying to display a list on my first view so added this in the first.h :
#import <UIKit/UIKit.h>
#interface argospineFirstViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *Journals;
IBOutlet UITableView *myTableView;
}
#property (nonatomic,retain) NSMutableArray *Journals;
#property (nonatomic, retain) UITableView *myTableView;
#end
and then i added this on my first.m :
#implementation argospineFirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Journals=[NSMutableArray arrayWithObjects:#"journal1",#"journal2",nil];
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(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] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
cell.text=[Journals objectAtIndex:indexPath.row];
return cell;
}
I am newbie so i don't really know what kind of connections i have got to make, i am also using a storyboard with a tableview dropped on the first view.
is there something i have to add to the delegate?
Any help?
Thank you for your time
Right click on the tableView in the storyboard. You will see "delegate" and "dataSource" under Outlets. Drag the bubble on the right of those to the view controller icon at the bottom of the view. This will make your viewcontroller the delegate and datasource for the table view if you don't want to do it programmatically.
Do not make property of your table view object.
Also,
in viewDidLoad method write:
myTableView.dataSource = self;
myTableView.delegate = self;
Tell me if it helps!
Use initWithStyle instead of initWithFrame for creating your cell.
In your storyboard, select your table view and open the Connections Inspector. Make sure that the delegate and datasource connections are linked to your argospineFirstViewController object.
in IBOutlet set delegete and datasource of the tableview to filesOwner
you use cell.text for show the data i think its not work in tableview just try this line:-
cell.textlabel.text=[yourArrayname objectatindex:index.row];
no need to connect delegate you already define in protocol.

UITableView Custom Cell Throwing SIGABRT Error

I am trying to create a custom cell for my UITableView. I am using Xcode 4.2 and using the storyboard along with ARC.
I have created a class to represent the custom cell like so:
ResultsCustomCell.h
#import <UIKit/UIKit.h>
#interface ResultsCustomCell : UITableViewCell
{
IBOutlet UILabel *customLabel;
}
#property (nonatomic, retain) IBOutlet UILabel* customLabel;
#end
ResultsCustomCell.m
#import "ResultsCustomCell.h"
#implementation ResultsCustomCell
#synthesize customLabel;
#end
I have then implemented the UITableView method in my view controller as follows:
ViewController.m
#import "ViewController.h"
#import "ResultsCustomCell.h"
#implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
// Set the number of items in the tableview to match the array count
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
// Populate TableView cells with contents of array.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
myCell.customLabel.text = #"helloWorld";
return myCell;
}
// Define height for cell.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
#end
The application builds successfully but then crashes instantly and give me the following error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
What part am I missing out?
This is an irritating problem because the debugger gives you no clue as to what happened. It will break on the exception and then simply shut down if you click Continue Execution.
This drove me nuts for 30 minutes until I right clicked on a UILabel in the custom cell that was giving me problems, which revealed the answer: One of the controls in your cell has (or had, as you apparently fixed it, perhaps without noticing) a spurious outlet. By "spurious outlet" I mean one that is wired to an IBOutlet property that no longer exists in your class. In my case I had changed the name of the property and rewired it, but had forgotten to remove the old referencing outlet connection.
As soon as I removed the offending outlet the problem went away.
You forgot to create a new UITableViewCell when it's not dequeued
// Populate TableView cells with contents of array.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (myCell == nil) {
myCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
myCell.customLabel.text = #"helloWorld";
return myCell;
}
You have forgotten to add the reuse identifier ("CellIdentifier") in the storyboard against your prototype cell, so when it tries to create a new cell, there is no prototype with that identifier in the storyboard, and it returns nil.
#property (nonatomic, retain) IBOutlet UILabel* customLabel;
Automatic Reference Counting (ARC) forbids the explicit call of retain. Try to remove this.
As for the error, you do return a UITableViewCell, not your custom cell. Additionally, you never allocate your ResultsCustomCell.
- (ResultsCustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
ResultsCustomCell *cell = (ResultsCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ResultsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
cell.textLabel.text = #"Text";
return cell;
}
Furthermore, your subclass of UITableViewCell does not declare the (obviously obligatory) method init.
ResultsCustomCell.h:
#import <UIKit/UIKit.h>
#interface ResultsCustomCell : UITableViewCell
#property (strong, nonatomic) UILabel *myLabel;
#end
ResultsCustomCell.m:
#import "ResultsCustomCell.h"
#implementation ResultsCustomCell
#synthesize myLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
#end
EDIT: I saw, that you are using storyboard. My suggestion might or might not be of help to you. I have never used storyboard.
I'm not entirely sure what I have done to resolve my problem but It is now working correctly.
Here is what I currently have in my ViewController.m
Note: The ResultsCustomCell.h and .m are the same as above.
#import "ViewController.h"
#import "ResultsCustomCell.h"
#implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
}
// Set the number of items in the tableview to match the array count
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
// Populate TableView cells with contents of array.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
// Make sure there are no quotations (") around the cell Identifier.
ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
myCell.customLabel.text = #"helloWorld";
return myCell;
}
// Define height for cell.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
#end
I then made sure the following things where correct:
The table view cell has the correct custom class (ResultsCustomCell).
The cell Identifier for the UITableViewCell was correct in Interface Builder.
The UILabel in interface builder was correctly linked to the IBOutlet.
The UITableView view had the correct controller (ViewController)
After using this code and checking all of the above it appears to work.
I was also fighting this error with the same setup as yours. I realized that it, for me, was caused by the new "Auto layout" feature of iOS6. To disable it I opened the custom cell xib, and then on the right hand side Utilities I opened the File inspector tab (the tab to the far left). In there I could un-check "Use Autolayout".
I created this exception by mistakenly registering the identifier with the wrong class type.
//The Identifier is register with the wrong class type
self.listView.register(CustomCellClass1.self, forCellReuseIdentifier: "CustomCellClass2")
And then dequeuing and casting to a different class type:
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellClass2", for: indexPath) as! CustomCellClass2

can't see when using UINib

been confused about this for over two hours, so maybe someone can point me at the right direction...
i have a navigation bar with a tableViewController under it. once the first row is selected i am pushing a new tableViewController that loads up custom table cells with the new and shiny UINib object.
cellForRowAtIndexPath is called and i allocated a new row, set up the values of it's two UILabel correctly, and return the cell.
however - the table view is completely empty. if i replace the custom cell with a regular table cell, i see the cell. what the hell is going on here?
some code:
in viewdidLoad:
self.cellNib = [UINib nibWithNibName:#"DetailCell" bundle:nil];
in cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"detailCell";
DetailCell* cell = (DetailCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[self.cellNib instantiateWithOwner:self options:nil];
cell = tmpCell;
self.tmpCell = nil;
}
cell.fieldName.text = #"field title";
cell.fieldValue.text = #"field value";
return cell;
}
and the custom cell (that has a xib file associated with it as well):
#interface DetailCell : UITableViewCell {
IBOutlet UILabel* fieldName;
IBOutlet UILabel* fieldValue;
}
#property (nonatomic, retain) IBOutlet UILabel* fieldName;
#property (nonatomic, retain) IBOutlet UILabel* fieldValue;
#end
thanks for your help.
for anyone following this thread, i found the issue to be with a missing cell identifier. the value you define at rowAtIndex needs to be entered using IB for the xib file. there is an identifier field.

UITableView works in 3.2 but no tableview methods are firing in 4.0 (simulator & device)

I just came across a situation where my implementation of a tableview is working in 3.2 but fails to fire any of the tableview methods in iOS 4. The view controller in question is defined as a UIViewController and is setup to adopt the UITableViewDataSource and UITableViewDelegate protocols, and I'm hooking the tableview up as the view's datasource and delegate via IB.
Definition in .h:
#interface FooViewController : UIViewController <UITableViewDataSource,
UITableViewDelegate> {
IBOutlet UITableView *itemTable;
}
#property (nonatomic, retain) IBOutlet UITableView *itemTable;
.m:
#synthesize itemTable;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(#"numberOfSectionsInTableView fired!");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
NSLog(#"numberOfRowsInSection fired!");
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];
}
return cell;
}
This appears to work fine in < 4.0, but in 4 it just simply bypasses firing any of these methods. Am I by chance doing it incorrectly and being caught up by something being fixed in 4.0?
Do you have the dataSource and delegate set for the itemTable? The rest of your code seems to look fine. Another question is why use the UiViewController instead of using the UITableViewController
I've been using the UITableViewController and it works quite well, and you can still just implement the simple basic methods you have implemented.