我想要做的是检测 UITableView
的哪些部分是可见的,然后根据可见的部分更改日历的日期。
问题是,通常有多个部分可以同时查看,我只想根据 visibleRowIndexes
中出现的第一个部分索引更改日期,而不是全部。
这是我当前的实现(注意:我在 cellForRowAtIndexPath 中运行此函数):
-(BOOL)whatSectionsAreVisible {
NSArray *visibleRowIndexes = [self.agendaTable indexPathsForVisibleRows];
for (NSIndexPath *index in visibleRowIndexes) {
NSNumber *daySection = @(index.section);
static NSDateFormatter *dateFormatter = nil;
if(!dateFormatter){
dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = @"yyyy-MM-dd"; // Read the documentation for dateFormat
}
// Here is where I will map every index.section to an NSDate
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:daySection.intValue]; // <<== Extract int from daySection
[comps setMonth:6];
[comps setYear:2015];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *date = [gregorian dateFromComponents:comps];
// Update the day selected according to visible section
[[NSNotificationCenter defaultCenter] postNotificationName:@"kJTCalendarDaySelected" object:date];
// Store currentDateSelected
[self.calendar setCurrentDateSelected:date];
NSLog(@"The visible section has an index of: %ld", (long)index.section);
if (index.section == 0) {
NSLog(@"index.row is indeed 0");
return YES;
}
}
return NO;
}
我尝试做类似 NSNumber *daySection = @(index[0].section);
而不是 NSNumber *daySection = @(index.section);
,但这似乎不起作用。
最佳答案
如果您只想要第一个可见行,请不要使用循环。获取 visibleRowIndexes
的第一个对象作为 NSIndexPath。然后从中获取部分。
关于ios - 如何仅引用indexPathsForVisibleRows中的第一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31012620/