z22
z22

Reputation: 10083

display data in UITableView on view navigation

i have 2 views-view1 and view2, one for entering data and another for displaying the entered data. I am able to display the entered data in view2 in labels. But how to display the data in a UITableView. Following is my code to display the data:

view2.m

@synthesize details; //details is an object of NSMutableArray.
    - (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    textLabel.text = self.name;
    lblCity.text = self.city;
    lblGender.text = self.gender;


}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [details count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

    cell.textLabel.text = [details objectAtIndex:indexPath.row];
    //cell.imageView.image = [UIImage imageNamed:@"creme_brelee.jpg"];
    return cell;

i debuuged and found that cellForRowAtIndexPath is never called.

Where am i getting wrong? How do i solve it?

Upvotes: 1

Views: 155

Answers (1)

Nitin Gohel
Nitin Gohel

Reputation: 49730

You need to create a NSMutableArray of display Data in to UITableViewCell like below and don't forget to declare UITableView delegates in .h file <UITableViewDataSource,UITableViewDelegate> and also connect your UITableView IBOutlet in your xib and also connect the delegate:

- (void)viewDidLoad
{
    [super viewDidLoad];
    arrCategorisation=[[NSMutableArray alloc] initWithObjects:@"DataOne",@"DataTwo",@"DataThree", @"DataFour",@"DataFive",nil];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrCategorisation.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier =[NSString stringWithFormat:@"%d",indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

            cell.textLabel.text=[arrCategorisation objectAtIndex:indexPath.row];

    }

    // Configure the cell...

    return cell;
}

#pragma mark - Table view delegate

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

}

your Table load data like:-

enter image description here

Upvotes: 2

Related Questions