0

I have an array with the following format:

[NSArray arrayWithObjects:
 [NSArray arrayWithObjects:@"number",@"name",@"date",@"about",nil],
 [NSArray arrayWithObjects:@"number",@"name",@"date",@"about",nil],
 [NSArray arrayWithObjects:@"number",@"name",@"date",@"about",nil],
 [NSArray arrayWithObjects:@"number",@"name",@"date",@"about",nil],
 nil];

I want to structure this data to load into my tableView.

Each row of the tableview should correspond to each row of the array, then the title of each cell should correspond to the subarray objectAtIndex 2 for the name.

3
  • It seems to me that this code is already 'structured' for a UITableView. Are you looking for the code to implement the creation of the UITableViewCells? Commented Aug 6, 2011 at 10:34
  • it looks like you're doing this the wrong way... you ned to be more specific about how you want the tableview to appear Commented Aug 6, 2011 at 10:35
  • Yes, i guess so, looking to setup the cells. Something like cell.textlabel.text = [array object at what?]] Because its an array inside an array. Commented Aug 6, 2011 at 10:36

1 Answer 1

2

Suppose your array is named myData:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  // Return the number of rows in the section.
  return [myData 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...
  NSArray *obj = [myData objectAtIndex:indexPath.row];
  cell.textLabel.text = (NSString*)[obj objectAtIndex:1]; //note: 0=>number; 1=>name,..

  return cell;
}

In favor of reusability, I would suggest to replace the subarrays with NSDictionaries so that you can get e.g. the name by calling [dict objectForKey:@"name"].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.