0

I have an app that adds Points of Interest (POIs) to a managedObjectContext. On the AddPOITableViewController, I have a segue that goes to a CategoryTableViewController and the CategoryTVC displays the existing categories in the managedObjectContext.

The navigation is as follows:

POIList-->AddPOIViewController-->CategoryTableViewController-->AddCategoryVC

Problem: As I don't have any categories in the managedObjectContext, there are none displayed on my CategoryTVC.

What I'd like to do: Create a special cell on CategoryTVC that segues to an AddCategoryTVC where I can create a new category in the managedObjectContext.

I know I'd need to add a special cell at index[0] that segues with I click. I'd like to drive that off the count of my category.count. I'd add the extra space for my special cell with numberOfRowsInSection (ex: category.count+1). For the segue to AddCategoryVC, I'd have to treat my "Add Category" cell differently (I'd make it index[0])

Any ideas how I'd do this would be greatly appreciated.

2 Answers 2

0

The indexPath object in the didSelectRowAtIndexPath callback tells you what cell was clicked. Simply check this object to see was it your "add category" cell, if so, perform the other segue. Like so:

- (void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == <myAddRowIndex>)
    {
         [self performSegueWithIdentifier:@"AddCategorySegue" sender:self];
    }
    else
    {
         [self performSegueWithIdentifier:@"pointOfInterestSegue" sender:self];
    }
}
0

I found this post helpful in getting my task done:

Combine static and prototype content in a table view

In this situation, I'm essentially creating a static cell at indexPath.row[0] and the rest of the content is dynamic and pulled from the managedObjectContext.

Here's what I ended up doing:

// Added this constant
#define NUMBER_OF_STATIC_CELLS 1

// Added these properties
static NSString *DynamicIdentifier = @"DynamicIdentifier";
static NSString *StaticIdentifier = @"StaticIdentifier";

// Tweaked numberOfRowsInSection to add an extra cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.locationCategories.count + NUMBER_OF_STATIC_CELLS;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row < NUMBER_OF_STATIC_CELLS) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:StaticIdentifier];

        // this sets up the static cell at the top
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:StaticIdentifier];
        }

        cell.textLabel.text = @"Add Category";
        cell.textLabel.textColor = [UIColor redColor];
        return cell;

    } else {

        // this sets up the dynamic cells below
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DynamicIdentifier forIndexPath:indexPath];
        // Configure the cell...
        NSManagedObject *locationCategory = [self.locationCategories objectAtIndex:indexPath.row];
        cell.textLabel.text = [locationCategory valueForKey:@"categoryName"];

        if (locationCategory == self.category.categoryName) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        return cell;
    }
}

This is where the segue takes place to my AddCategoryViewController.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // If there was a previous selection, unset the accessory view for its cell.
    NSManagedObject *currentCategory = self.category.categoryName;

    if (currentCategory != nil && indexPath.row > NUMBER_OF_STATIC_CELLS - 1) {
        NSInteger index = [self.locationCategories indexOfObject:currentCategory];
        NSIndexPath *selectionIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
        UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:selectionIndexPath];
        checkedCell.accessoryType = UITableViewCellAccessoryNone;
    }

    if (indexPath.row > NUMBER_OF_STATIC_CELLS - 1) {
        // Set the checkmark accessory for the selected row.
        [[tableView cellForRowAtIndexPath:indexPath]setAccessoryType:UITableViewCellAccessoryCheckmark];

        // Update the type of the location category instance
        self.category.categoryName = [self.locationCategories objectAtIndex:indexPath.row];

        // Deselect row
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    } else {
        [self performSegueWithIdentifier:@"addCategory" sender:nil];
        NSLog(@"addCategory segue fired from didSelectRowAtIndexPath method");
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.