objective-c – As in Table View using Segmented Control, you can change cells using Core Data

Question:

There is a Table View and Segmented Control, as well as Core Data in which data is stored. The data is moved to the table, but when you change the segment, you cannot change the data in the cells. I suspect that the problem lies here, but I can't figure out exactly where:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSInteger returnValue = 0;

    switch(self.segmentControl.selectedSegmentIndex)
    {
        case 0:
            returnValue = self.books.count;
            break;
        case 1:
            returnValue = self.discs.count;
            break;
        default:
            break;
    }

    return returnValue;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSManagedObject *book = [self.books objectAtIndex:indexPath.row];
    NSManagedObject *disc  = [self.discs objectAtIndex:indexPath.row];

    switch(self.segmentControl.selectedSegmentIndex)
    {
    case 0:

        [cell.textLabel setText:[NSString stringWithFormat:@"%@ ",
                                 [book valueForKey:@"title"]]];
        [cell.detailTextLabel setText:[book valueForKey:@"category"]];
        break;
    case 1:
        [cell.textLabel setText:[NSString stringWithFormat:@"%@ ",
                                 [disc valueForKey:@"title"]]];
        [cell.detailTextLabel setText:[disc valueForKey:@"category"]];
        break;
    default:
        break;
    }
    return cell;
}

Answer:

Greetings.

It is necessary to add a processing method (IBAction) for changing the value of UISegmentedControl. Inside the method, write:

[self.tableView reloadData];
Scroll to Top
AllEscort