找到你要的答案

Q:How to reference only the first object in indexPathsForVisibleRows

Q:如何参考的第一个对象indexpathsforvisiblerows

What I'm trying to do is detect which sections of a UITableView are visible, and then change the date of a calendar based on which are visible.

The issue is that there are typically multiple sections viewable at the same time, and I only want to change the date based on the first section index that appears in visibleRowIndexes, not all of them.

Here's my current implementation (Note: I run this function in 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;
}

I tried doing something like NSNumber *daySection = @(index[0].section); instead of NSNumber *daySection = @(index.section);, but that doesn't seem to work.

我想做的是检测一个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 = @(指标[ 0 ]。部分);而不是NSNumber * daysection = @(索引段);,但似乎并没有工作。

answer1: 回答1:

Don't use a loop if you just want the first visible row. Get the first object of visibleRowIndexes as an NSIndexPath. Then get the section from that.

如果你只想要第一个可见行,不要使用循环。得到visiblerowindexes第一对象作为一个nsindexpath。然后从那得到部分。

answer2: 回答2:

The indexPathsForVisibleRows method returns the index paths in ascending order. So, get rid of the for loop and execute the code in it only for the first object:

NSArray *visibleRowIndexes = [self.agendaTable indexPathsForVisibleRows];
NSIndexPath *index = [visibleRowIndex objectAtIndex:0];

方法返回的indexpathsforvisiblerows升序索引路径。因此,去掉for循环并仅在第一个对象中执行代码:

NSArray *visibleRowIndexes = [self.agendaTable indexPathsForVisibleRows];
NSIndexPath *index = [visibleRowIndex objectAtIndex:0];
ios  objective-c  uitableview