Jayshree
Jayshree

Reputation: 281

UITableView not getting populated

I have a peculiar problem. I created a normal Objective C class called SampleTable. I then extended UITableView instead of NSObject. Then in the initWithFrame constructor I initialized the table. I also made a NSMutableArray object for the datasource. I also conformed UITableViewDelegate and UITableViewDatasource. I have overridden the necessary methods also.

Now I made an object of this class in another class, and added the object as a subview. The tableView is getting drawn according to the CGRectMake() coordinates I gave to the initWithFrame constructor. But it is not getting populated with the data. I don't know what the problem is.

SampleTable.h

 #import <Foundation/Foundation.h>

 @interface SampleTable : UITableView { 

    NSMutableArray *ItemArray; 

 } 

@property (nonatomic,retain) NSMutableArray *ItemArray;

 -(NSMutableArray *) displayItemArray; 

@end

SampleTable.m

 #import "SampleTable.h"

 @implementation SampleTable

 @synthesize ItemArray;

 -(id)initWithFrame:(CGRect)frm {

     [super initWithFrame:frm];

     self.delegate=self;

     self.dataSource=self;

     [self reloadData];

     return self;

}

 -(NSMutableArray *) displayItemArray { 

    if(ItemArray==nil) { 

       ItemArray=[[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",nil];

    }

  return ItemArray;

 }

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 { 

     return 1; 

 }

   -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [ItemArray count];

}

 -(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

   if (cell==nil) {

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

    [cell autorelease]; 

   } 

   NSString *temp=[self.ItemArray objectAtIndex:indexPath.row]; 

   cell.textLabel.text = temp; 

  return cell; 

}

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

   NSLog(@"didselect");

}

 -(void) dealloc { 

     [ItemArray release];

     [super dealloc]; 

 }

  @end

Upvotes: 2

Views: 1401

Answers (3)

Till
Till

Reputation: 27597

You did never initialize the ItemArray within your code snippet. Remove the displayItemData method and change the initializer towards:


-(id)initWithFrame:(CGRect)frm 
{
    if ((self = [super initWithFrame:frm])) != nil)
    {
        self.delegate=self;
        self.dataSource=self;
        ItemArray=[[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
        [self reloadData];
    }
    return self;
}

You could also simply call that displayItemArray method within the initializer. I feel that method makes no sense as it stands and hence my recommendation to remove it altogether.

Without trying it myself, I am still pretty confident that you can also get rid of that [self reloadData] within the initializer.

Upvotes: 3

Ralf Edmund
Ralf Edmund

Reputation: 1035

During the -(id)initWithFrame:(CGRect)frm method your code calls -reloadData on the UITableView. At this point in time your ItemArray is not available.

Therefore, when UITableView calls it's delegates -numberOfSectionsInTableView: and -tableView:numberOfRowsInSection: methods to get information on what to display in the view, they return one section with zero rows.

Nothing displayed!

It might be a (one) solution to change your initialization of the ItemArray:

-(NSMutableArray *) displayItemArray {
    if(ItemArray==nil) { 
        ItemArray=[[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
        [self reloadData];
    }
    return ItemArray;
}

Upvotes: 1

Mihir Mehta
Mihir Mehta

Reputation: 13843

print value of

[self.ItemArray objectAtIndex:indexPath.row]; 

in NSLog... check wether it prints valid value or not....It seems that array's object releasing somewhere before it..

Upvotes: 0

Related Questions