ios - 如何仅引用indexPathsForVisibleRows中的第一个对象

标签 ios objective-c uitableview

我想要做的是检测 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/

相关文章:

ios - 调用 Firebase Observer 后重新加载 TableView

ios - 类型 'inout UIButton' 不符合协议(protocol) 'ReactiveCompatible'

ios - 使用 RubyMotion 选择 iOS 模拟器设备类型

ios - 镜像 UIView

iphone - If 语句不适用于空格字符

ios - PDF 第一页在 iOS 中不显示

ios - 给定不同字段的键,从字典中获取值

iOS 如何制作图书应用程序?导航 Controller + 文本 + 索引

objective-c - 微博等iOS 6 ActivityViewController定制

ios - 从自定义 TableViewCell 中的按钮访问主视图