15

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?

6 Answers 6

59

Try this

[NSIndexPath indexPathForRow:NSNotFound inSection:section]
4
  • 2
    Works great on iOS10/11. Elegant solution! Dec 26, 2017 at 15:34
  • Thanks! Worked for me Nov 12, 2018 at 13:30
  • how to use it with tableview ?!!
    – zakaria
    Jul 15, 2019 at 8:49
  • For Swift version of this answer, see @VictorCasé's answer below.
    – Eric
    Mar 4, 2022 at 20:32
21

Swift 3.0

DispatchQueue.main.async {
   let indexPath = IndexPath(item: 'item', section: 'section')
   self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}
14

Swift version of @black-sheep answer:

let sectionIndexPath = IndexPath(row: NSNotFound, section: section)
0
9

When your section has no cells, for example it has just a header or footer, you can use one of the following options:

let sectionRect = yourTable.rect(forSection: sectionNumber)
yourTable.setContentOffset(CGPoint(x: sectionRect.origin.x, y: sectionRect.origin.y), animated: true)

or

let sectionRect = yourTable.rect(forSection: sectionNumber)
yourTable.scrollRectToVisible(sectionRect, animated: false)
7

Row is must be int value and that variable define in .h and use this variable in anywhere in .m class that you are want to scroll on particular position.

You can scroll UITableView on particular section and also you can scroll to section's particular index cell using following code:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[MytblView scrollToRowAtIndexPath:indexPath 
                     atScrollPosition:UITableViewScrollPositionTop 
                             animated:YES];
1

You have to find/calculate the section and row that you want to scroll

NSInteger row = ??;//you have to find/calculate which row you want to show
NSInteger section = ??;//you have to find/calculate which row of the sections you want to show
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];

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

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.