views:

1098

answers:

1

Hi All,

I am pretty new to Iphone development . so please bear me if I ask some very simple questions.

In my application I have multiple views(i.e. .xib files). On clicking the button on the main view(CouponWebsiteViewController.Xib) the application should load the second view(BrowseList.Xib) which contains a UITable and I have to populate that UITable with some data. I have written following code to populate the data on BrowseList.m file:

  • (void) viewDidLoad{ arrayData = [[NSArray alloc] init]; arrayData = [arrayData arrayByAddingObject:@"dfsgdf"]; arrayData = [arrayData arrayByAddingObject:@"aaaaaa"]; self.lblNewScreen.text = [arrayData objectAtIndex:0]; [super viewDidLoad]; }

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

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

// 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] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; }

NSString *cellValue = [arrayData objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
[tableView setEditing:YES animated:YES];
return cell;

}

But It is not populating the data in table , when I debug this code I found that it is not executing cellForRowAtIndexPath method but it is debugging numberOfRowsInSection and numberOfSectionsInTableView methods.

But the interesting thing is when I write the same code on the CouponWebsiteViewController.m (i.e. on main view) then it is populating the data in table.

The point is this code is working fine on the main view but it does not work for the other views.

Can any one tell me what am I missing or any other way to populate the UITable on views other than the main view.

Thanks in Advance. Gaurav

A: 

I'm not positive but here are some ideas.

  • I think when you put that array code in viewDidLoad it's too late. ViewDidLoad is executed after the data has already been placed in the table. Try putting this code in the initWithStyle method or whatever init method you have. Or you could even put it int the ViewWillAppear method, but then make sure to follow this next suggestion.
  • The other thing you could try is, just call [self.tableView reloadData] at the end of the viewDidLoad method. Although that's not as ideal as the first suggestion.

I copied your original code below, but I put <pre><code> tags at the beginning and </code></pre> tags at the end to make it easier to view on this website.

Your Original Code:

- (void) viewDidLoad{ 
  arrayData = [[NSArray alloc] init]; 
  arrayData = [arrayData arrayByAddingObject:@"dfsgdf"]; 
  arrayData = [arrayData arrayByAddingObject:@"aaaaaa"]; 
  self.lblNewScreen.text = [arrayData objectAtIndex:0]; 
  [super viewDidLoad];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
// Customize the number of rows in the table view. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [arrayData count]; }
// 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] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; }
NSString *cellValue = [arrayData objectAtIndex:indexPath.row]; cell.textLabel.text = cellValue; [tableView setEditing:YES animated:YES];
return cell; }

Andrew