如何以编程方式使UITableView滚动到特定部分 [英] How to programmatically have UITableView scroll to a specific section
本文介绍了如何以编程方式使UITableView滚动到特定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的日历应用程序中,我希望UITableView可以根据所点击的日期滚动到特定部分.当前实现如下:
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];
}
这给出了一个错误,指出row
是一个未声明的标识符,并且我希望它始终使用节索引而不是行.我将如何实施呢?
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?
推荐答案
尝试一下
[NSIndexPath indexPathForRow:NSNotFound inSection:section]
这篇关于如何以编程方式使UITableView滚动到特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文