Question

I'm coming across an issue which I am not sure how to get working.

I have a Table view controller (TV1) which is embedded in a Tab Bar with 2 tabs. TV1 is populated by the user clicking on a navigation bar button item to add in the name, event, amount and date. When they click save, the table view gets populated through the use of NSFetchedResultsController and Core data.

The first tab displays the entries in Chronological order. The second displays only a list of events, so instead of seeing everything, you only see the unique events that you have created. If you go into an event, you see all "transactions" with that event. So if you have 4 Events (Birthday, Wedding, Anniversary, New Year), you will see that in the second tab. If you click on Wedding, you'll see all of the transactions which have an event of Wedding.

That works well so far. I want to introduce a new feature. If the user goes into Wedding (where they see all the transactions with the Wedding event) and clicks the plus button in the Navigation Bar, they'll be taken to the Add Entry screen where they can quickly enter in some new entries.

The reason for bringing this plus button here as well is because I want the Event and the Date to already be populated, because that will always be constant. It's only the name and amount that the user has to fill in.

EDIT (Changing the requirements slightly of the question- still the same requirement but now I understand what I require).

The Wedding table view (selected event) has sections and rows. The rows are the individual transactions, and the section is the date in the form of a string (converted from date (transaction.date.dateOfEvent).

What I want is the following:

1) Take the first section string (there may be multiple sections, so I only want the first one)

2) Convert it to a date

3) Push that date over in prepareForSegue to the Add Entry

4) Populate the DatePicker with the passed in date

My section titles are currently:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 10)];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.text = [sectionInfo name];
    return label;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[[self.fetchedResultsController sections] objectAtIndex:section] name];
}

My NSFetchedResultsController method is:

- (NSFetchedResultsController *)fetchedResultsController
{
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    if (_fetchedResultsController != nil)
    {
        return _fetchedResultsController;
    }
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Transaction" inManagedObjectContext:managedObjectContext];
    fetchRequest.entity = entity;
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"dates.dateOfEvent" ascending:NO];
    fetchRequest.sortDescriptors = [NSArray arrayWithObject:sort];
    fetchRequest.fetchBatchSize = 20;
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"occasion = %@", self.occasion];
    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"sectionDateFormatter" cacheName:nil];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;
    return _fetchedResultsController;
}

The sectionNameKeyPath is in the Transaction NSManagedObject Subclass which converts the date from the date picker to a string:

-(NSString *)sectionDateFormatter
{
    return [NSDateFormatter localizedStringFromDate:self.dates.dateOfEvent
                                          dateStyle:NSDateFormatterLongStyle
                                          timeStyle:NSDateFormatterNoStyle];
}

So I would need to achieve 1 - 4 as stated above. The first thing is obtaining whatever date (string) is in the first section at the top, convert it to a date and pass that forward.

This seems straightforward but I'm fairly new to programming and any guidance would really be great with some sample code for achieving this.

Was it helpful?

Solution

As you said

1 point answer is

get the section title

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0] ;
NSString *sectionTitle = [tableviewObj tableView:tableViewObj titleForHeaderInSection:indexPath.section];

4 point answer is

there is a method in UIPickerView

- (void)setDate:(NSDate *)date animated:(BOOL)animated;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top