Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array to UITableView

This sounds like it's easy and from the tutorials it looks very easy.

I have followed the tutorials from their words EXACTLY and still can not get the array to display on my UITableView.

Here is my code.

- (void)viewDidLoad
{
   [super viewDidLoad];
   // Do any additional setup after loading the view from its nib.
   salesArray = [[NSMutableArray alloc] init];

  //Add items
  [salesArray  addObject:@"Credit"];
  [salesArray  addObject:@"Debit"];
  [salesArray  addObject:@"EBT"];


  //Set the title
  self.navigationItem.title = @"Sale Type's";
}

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

-(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];
  }
  NSString *value = [salesArray objectAtIndex:indexPath.row];
  cell.textLabel.text = value;
  return cell;
}

And yes, I have declared it in the .h file and the .m file.
Any help in any directions would be great, thank you!

like image 656
Keeano Avatar asked Dec 21 '22 09:12

Keeano


2 Answers

Declare your salesArray as your property and in the the cellForRowAtIndexPath, use

cell.textLabel.text = [self.salesArray objectAtIndex:indexPath.row];
like image 125
Legolas Avatar answered Jan 09 '23 12:01

Legolas


I am assuming that your view controller extends UITableViewController? If so, you just need to add this line at the end of your viewDidLoad and/or viewDidAppear methods:

[self.tableView reloadData];

Also, I would use self.salesArray instead of salesArray. Not exactly sure what the difference is, but I have had issues in the past not prepending "self" to some of my variables.

like image 40
armohan Avatar answered Jan 09 '23 11:01

armohan