Gabrielle
Gabrielle

Reputation: 4981

No text on tableView in Iphone

I'm new to Iphone and I need some help to display text on tableview.

Here is my code:

.h

@class MainMenuViewController;

@interface RecherchePartenaireViewController : UIViewController <UINavigationControllerDelegate> {

    MainMenuViewController *mainMenuViewController;

    UINavigationController *navigationController;

    GradientButton *rechercheButton;

    RecherchePartenaireResultatListeViewControleur *recherchePartenaireResultatListeViewControleur;

    IBOutlet UITableView *categorystable;
    NSMutableArray *listData;
}

@property (nonatomic, retain) IBOutlet MainMenuViewController *mainMenuViewController;

@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@property (nonatomic, retain) IBOutlet GradientButton *rechercheButton;

@property (nonatomic, retain) IBOutlet RecherchePartenaireResultatListeViewControleur *recherchePartenaireResultatListeViewControleur;

@property (nonatomic, retain) IBOutlet UITableView *categorystable;
@property(nonatomic, retain) NSArray *listData;

@end

and in .m I have :

@synthesize mainMenuViewController, navigationController, rechercheButton, recherchePartenaireResultatListeViewControleur,listData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{

    [mainMenuViewController release];
    [navigationController release];
    [rechercheButton release];

    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    listData = [[NSMutableArray alloc] initWithObjects:@"iPhone", @"iPod", @"iPad",nil];
    NSLog(@"hey %@",listData);

    [rechercheButton useRedDeleteStyle];

    // Do any additional setup after loading the view from its nib.

}

- (void)viewDidUnload
{
    self.rechercheButton = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


// Customize the appearance of table view cells.
- (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] autorelease];


    }

    // Set up the cell...
    cell.textLabel.text = @"label";

    /*[[cell textLabel] setText: [[listData objectAtIndex:indexPath.row] valueForKey:@"name"]];*/


    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return listData;
}

@end

and I get nothing on tableView.Where is my mistake? I have seen many examples on net but I cant see what I do wrong. Can anyone help me?

Upvotes: 0

Views: 133

Answers (6)

Srinivas
Srinivas

Reputation: 983

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [listData count];//that many times cellFoeRowAt Index method Called.
  }
  in above case you are returning Array instead OF Count.

cheers

Upvotes: 1

Zoleas
Zoleas

Reputation: 4879

In your Xib File, have you set the delegate and dataSource of your tableView as your viewController ?

Upvotes: 3

Mobile App Dev
Mobile App Dev

Reputation: 1814

you have to make change below.

- (void)viewDidLoad
{
    [super viewDidLoad];

    listData = [[NSMutableArray alloc] initWithObjects:@"iPhone", @"iPod", @"iPad",nil];
    NSLog(@"hey %@",listData);

    [rechercheButton useRedDeleteStyle];

    // Do any additional setup after loading the view from its nib.

   [categorystable reloadData];
}

And also change.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [listData count];
}

Upvotes: 2

EXC_BAD_ACCESS
EXC_BAD_ACCESS

Reputation: 2707

Change like this,

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [listData count];
}

Upvotes: 2

Nekto
Nekto

Reputation: 17877

If you want to use standard style of cells in your UITableView then you should replace line where you create new cell with this one, for example:

replace

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

with

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

You can also select another predefined style for cell: UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle

Also you have error an in method numberOfRowsInSection:. It should look like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section        
{
    return [listData count];
}

Upvotes: 2

Madhu
Madhu

Reputation: 2384

If you mean your tableView is visible but no cells / content is visible, perhaps you could try setting a value for;

EDIT: Have this in your .m which will set the row height to 44px.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44.0;
}

Also agree with the other answers that you need to be returning [listData count] instead of just listData for that method.

Upvotes: 1

Related Questions