Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c ScrollToPosition始终滚动到“我的分区”中的最后一行_Objective C_Ios_Uitableview_Scroll - Fatal编程技术网

Objective c ScrollToPosition始终滚动到“我的分区”中的最后一行

Objective c ScrollToPosition始终滚动到“我的分区”中的最后一行,objective-c,ios,uitableview,scroll,Objective C,Ios,Uitableview,Scroll,我有一个分区的桌面视图。每节为一个月。所以我有6月、7月、8月的部分 我现在要做的是,当tableview出现时,它会立即向下滚动到今天的月份。我有下面的函数 -(void)scrollToPosition{ NSDate *now = [NSDate date]; NSString *strDate = [[NSString alloc] initWithFormat:@"%@",now]; NSArray *arr = [strDate componentsSeparat

我有一个分区的桌面视图。每节为一个月。所以我有6月、7月、8月的部分

我现在要做的是,当tableview出现时,它会立即向下滚动到今天的月份。我有下面的函数

-(void)scrollToPosition{
  NSDate *now = [NSDate date];
    NSString *strDate = [[NSString alloc] initWithFormat:@"%@",now];
    NSArray *arr = [strDate componentsSeparatedByString:@" "];
    NSString *str;
    str = [arr objectAtIndex:0];
    NSLog(@"strdate: %@",str); // strdate: 2011-02-28

    NSArray *arr_my = [str componentsSeparatedByString:@"-"];

    NSInteger month = [[arr_my objectAtIndex:1] intValue];
    NSLog(@"month - 5 %d",month -5);

    NSIndexPath *path = [NSIndexPath indexPathForRow:1 inSection:month -5];
    NSLog(@"path = %@",path);
    [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionMiddle animated:YES];

}
我之所以选择第5个月,是因为我的tableview从6月份开始。我的问题是,它会向下滚动到节的最后一行,而不是第一行。有人能帮我吗

亲切问候,

编辑

My tableview looks likes this. 

---Section 1: June -----
    - row 1 (12-06-2012)
    - row 2 (14-06-2012)
    - row 3 (20-06-2012)
    - row 4 (22-06-2012)
---Section 2: July -----
    - row 1 (2-07-2012)
    - row 2 (14-07-2012)
    - row 3 (21-07-2012)
    - row 4 (27-07-2012)
---Section 3: August -----
    - row 1 (2-08-2012)
    - row 2 (14-08-2012)
---Section 4: September -----
    - row 1 (17-09-2012)
---Section 5: Oktober -----
    - row 1
    - row 2
    - row 3
    - row 4
---Section 6: November -----
    - row 1
    - row 2
    - row 3
    - row 4
---Section 7: December -----
    - row 1
    - row 2
    - row 3
---Section 8: January -----
    - row 1
    - row 2
编辑:屏幕截图


在这里,您可以看到滚动后我的tableview的屏幕截图

问题是,截面索引开始于0而不是1

因此,当您返回的月份为6您的呼叫:

NSIndexPath *path = [NSIndexPath indexPathForRow:1 inSection:1]; // month - 5 = 1
[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
但你应该打电话:

NSIndexPath *path = [NSIndexPath indexPathForRow:1 inSection:0];
因此,您可以滚动到您实际想要滚动到的部分下方1。这就是为什么它看起来会滚动到上一节的最后一行

month-5
替换为
month-6
,它应该可以正常工作

顺便说一下,我建议修改您的代码以检索当前月份,如下所示:

NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM"];
int month = [[dateFormatter stringFromDate:now] intValue];
//...

如果月份的返回值为2,则它将滚动到第3节第1行。我希望您在这里创建了正确的indexath。你能详细说明你想在哪一行滚动到哪一部分吗?@PraveenS是的,我知道这个月有一些问题。我需要用一个if函数来解决它。但是这个部分目前是行的,我对这行有一个问题。@ PraveNes我已经编辑了我的问题,我的TabLVIEW看起来是怎样的,它是如何滚动到节的底部的,是在滚动之后的表中间的最后一行吗?这就是十二月部分第二行的索引路径。(遵循您的数据源列表)你正在使用UITababVIEW ScRealPosiType中间-单元格显示在表格中间。如果你想在表格的顶部看到十一月部分的第一行,节索引必须是代码> -1 < /Cord>,你要使用<代码> UITableViewScrollPositionTop <代码>。这对你有意义吗?