Как структурировать массив в UITableView? - PullRequest
0 голосов
/ 06 августа 2011

У меня есть массив в следующем формате:

[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];

Я хочу структурировать эти данные для загрузки в мой tableView.

Каждая строка в табличном представлении должна соответствовать каждой строкемассив, тогда название каждой ячейки должно соответствовать subarray objectAtIndex 2 для имени.

1 Ответ

2 голосов
/ 06 августа 2011

Предположим, ваш массив имеет имя 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;
}

. В целях многократного использования я бы предложил заменить подмассивы на NSDictionaries, чтобы вы могли получить, например, имя, вызвав [dict objectForKey:@"name"].

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...