简体   繁体   中英

UITableView section and index by core data

i use below code to binding tableview cell by "contacts" entity

- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context=nil;
    id delegate=[[UIApplication sharedApplication]delegate];
    if ([delegate performSelector:@selector(managedObjectContext)])
    {
        context=[delegate managedObjectContext];
    }
    return context;
}


- (void)viewDidAppear:(BOOL)animated
{

    //fetching contact into tableview
    NSManagedObjectContext *moc=[self managedObjectContext];
    totalFetch=[[NSFetchRequest alloc]initWithEntityName:@"Contacts"];
    // 4 - Sort it if you want
    totalFetch.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]];

    _contacts=[[moc executeFetchRequest:totalFetch error:nil]mutableCopy];
    [self.myTable reloadData];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
         {
    //create and initializetion cell
    static NSString *cellIdentifier=@"cell";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    NSManagedObject *contact=[_contacts objectAtIndex:indexPath.row];

    //implementing tableViewCells with Contact attribs
    [cell.textLabel setText:[NSString stringWithFormat:@"%@",[contact valueForKey:@"name"]]];
    [cell.detailTextLabel setText:[contact valueForKey:@"phoneNum"]];
    UIImage *image = [UIImage imageWithData:[contact valueForKey:@"photo"]];
    [cell.imageView setImage:image];

    return cell;

    }

now i want to add index and section to mytableview by first letter of "name" field in "contacts" entity, Please put your answers according to my code, thanks

You need to implement the following datasource methods for UITableView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

The first two are standard and implement the cells that are going to be displayed in the table view.

The third one is the number of sections the table view should be divided up into. For something like this I would use an 'alphabet array' ie an array with all the letters of the alphabet in eg:

NSMutableArray *alphabetArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 26; i++) {
    char letter = 'A';
    letter += i;
    [alphabetArray addObject:[NSString stringWithFormat:@"%c", letter]];
}

You can return alphabetArray.count for the third datasource method.

The fourth method requests the section number for the section title, all you need to do here is return: [alphabetArray indexOfObject:title]; .

The fifth method is asking for the title for a specific section so you can return: alphabetArray[section]; here.

Finally the sixth method is asking for all the titles in an array so you can just return alphabetArray here .

In order to get the number of rows in a section you need to break down your contacts array by starting letter. To do this you can use predicates eg

NSString *letter = alphabetArray[indexPath.section];
NSArray *filteredContacts = [contacts filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", letter]];
return filteredContacts.count;

You can apply the same logic to get the contact to display in a cell:

NSString *letter = alphabetArray[indexPath.section];
NSArray *filteredContacts = [contacts filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", letter]];
Contact *contact = filteredContacts[indexPath.row];

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM