George Morgan
George Morgan

Reputation: 677

UITableView not displaying data from array

Code:

In .h:

    NSMutableArray *contentArray;

I'm declaring my array.


In .m

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        contentArray = [[NSMutableArray alloc] initWithObjects:@"view", @"browse", @"create", nil];

        // Uncomment the following line to preserve selection between presentations.
        // self.clearsSelectionOnViewWillAppear = NO;

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    }

I'm setting it up in my view did load.

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

I'm setting the number of rows to the array count.


    - (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];
        }

        // Configure the cell...

        [[cell textLabel] setText:[contentArray objectAtIndex:indexPath.row]];

        return cell;
    }

Nothing. But if I do "[[cell textLabel] setText:@"Hello World."];" instead, it works fine.

Upvotes: 0

Views: 6153

Answers (5)

Eddie Callahan
Eddie Callahan

Reputation: 27

Make sure that your .h implements the tableview delegate and datasource... should look like this

 @interface myClass : UITableViewController <UITableViewDelegate, UITableViewDataSource>

then make this code in your ViewDidLoad method

 [self.tableView setDelegate:self];
 [self.tableView setDataSource:self];

Upvotes: 0

Bhoopi
Bhoopi

Reputation: 6593

simply use following line of code //initlize nsarray with objects.

-(void)viewDidLoad
{
NSArray *practice_ArrayForDisplayingEx=[NSArray alloc]initWithObjects:@"bhupi",@"bhupi",@"bhupi",@"bhupi",nil];

}


//then use in tableview.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
   // UIButton *practice_takeTest_button;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]autorelease];
         }


    cell.textLabel.text =[practice_ArrayForDisplayingEx objectAtIndex:indexPath.row];
    cell.textLabel.font=[UIFont fontWithName:@"Arial" size:15];



    return cell;
}

make sure you included UITableViewDelegate & UITableViewDataSourse prtocol in .h file.

hope it will help you..

Upvotes: 0

Jyoti Kumari
Jyoti Kumari

Reputation: 45

Your code in .m file is fine. Write property .h file synthesize and dealloc your NSMutable array .m file it may be help you.

Upvotes: 0

slonkar
slonkar

Reputation: 4085

Are you at least getting 3 empty rows in table ? If yes then just change your code

NSString *tempString = [your_array objectAtIndex:indexPath.row];
cell.textLabel.text = tempString; 

If you are not even getting empty strings then make property of your array in .h file. Synthesize it in .m (also release it in delloc function) and finally in viewDidLoad right following

NSMutableArray tempContentArray = [[NSMutableArray alloc] arrayWithObjects:@"view", @"browse", @"create", nil];
self.contentArray=tempArray;

and then write following code to get cell title

NSString *tempString = [self.contentArray objectAtIndex:indexPath.row];
cell.textLabel.text = tempString; 

Upvotes: 1

MoFuRo
MoFuRo

Reputation: 231

Try to initialize your Array in your init method or call reloadDataon your UITableView after you set your Array.

Upvotes: 0

Related Questions