mardi 23 juin 2015

How to programmatically have UITableView scroll to a specific section

In my calendar app, I want my UITableView to scroll to a specific section based on which date is tapped. Current implementation is below:

- (void)calendarDidDateSelected:(JTCalendar *)calendar date:(NSDate *)date
{
    // Format NSDate so it can be compared to date selected
    static NSDateFormatter *dateFormatter = nil;
    if(!dateFormatter){
        dateFormatter = [NSDateFormatter new];
        dateFormatter.dateFormat = @"yyyy-MM-dd"; // Read the documentation for dateFormat
    }

    // Set formatted dates
    NSDate *juneSixteenth = [dateFormatter dateFromString:@"2015-06-16"];
    NSDate *juneSeventeenth = [dateFormatter dateFromString:@"2015-06-17"];

    // Set date selected, so agendaTable knows which event to show
    if([juneSixteenth compare:date] == NSOrderedSame){
        NSLog(@"You picked June 16th");
        self.datePicked = [NSNumber numberWithInt:16];
    }
    else if([juneSeventeenth compare:date] == NSOrderedSame){
        NSLog(@"You picked June 17th");
        self.datePicked = [NSNumber numberWithInt:17];
    }
    else {
        _datePicked = nil;
        NSLog(@"Date: %@", date);
    }
    [self.agendaTable reloadData];
}

I found a similar question that says to do it like so:

       if([juneSixteenth compare:date] == NSOrderedSame){
            NSLog(@"You picked June 16th");
            self.datePicked = [NSNumber numberWithInt:16];

            //"row" below is row selected in the picker view
            NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:row];

            [self.agendaTable scrollToRowAtIndexPath:ip
                          atScrollPosition:UITableViewScrollPositionNone
                                  animated:YES];
        }

This gives an error stating that row is an undeclared identifier, and I want it to use the section index rather than the row anyway. How would I implement this?

See more ..

Aucun commentaire:

Enregistrer un commentaire