Да, это легко, хотя есть миллион способов сделать это.
Ваш контроллер представления должен быть "источником данных" UITableView
и возвращает информацию о количестве строка затем содержимое каждой отдельной строки.
В табличном представлении есть понятие "раздел", вы можете выбрать один для каждой категории.
Например, вы можете создатьNSFetchedResultsController
, чтобы найти категории, которые вы хотите отобразить, и использовать их для заполнения разделов табличного представления, и тогда каждая категория будет иметь отношение статей ко многим для заполнения строк в каждом разделе.
Примерно так должно начаться (при условии, что ваши категории и сущности статьи содержат свойство title
):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// return the number of categories
[[self.categoryResultsController fetchedObjects] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// return the title of an individual category
[[self.categoryResultsController.fetchedObjects objectAtIndex:section] valueForKey:@"title"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// return the number of articles in a category
MyCategory *category = [self.categoryResultsController.fetchedObjects objectAtIndex:section];
return category.articles.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// fetch a cached cell object (since every row is the same, we re-use the same object over and over)
static NSString *identifier = @"ArticleCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}
// find the category and article, and set the text of the cell
MyCategory *category = [self.categoryResultsController.fetchedObjects objectAtIndex:indexPath.section];
cell.textLabel.text = [[category.articles objectAtIndex:indexPath.row] valueForKey:@"title"];
return cell;
}
Вы можете прочитать документацию по этим методам, чтобы выяснить, как настроить его дальше..