2014-06-10 109 views
0

我想實現的是「捕捉到單元格」效果在我的UITableView。 我的UITableView有3個相同大小的單元格,但由於某些原因,UITableView總是捕捉到單元格0或單元格1,並且不會捕捉到第三個單元格。無論你滑得多麼艱難,它的錯誤也會降低50分左右。我使用最明顯的代碼來創建效果,所以我不明白爲什麼它不起作用。UITableView捕捉到單元格

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { 
    NSIndexPath *pathForTargetTopCell = [self.tableView indexPathForRowAtPoint:CGPointMake(CGRectGetMidX(self.tableView.bounds), targetContentOffset->y)]; 
    DLog(@"indexPathForRow %d", pathForTargetTopCell.row); 
    targetContentOffset->y = [self.tableView rectForRowAtIndexPath:pathForTargetTopCell].origin.y; 
} 

有什麼想法可能會出錯?

編輯: 我懷疑這與頂部的狀態欄和導航欄有關,因爲它關閉了正好64點。仍然沒有解釋爲什麼它不承認,雖然最後一個單元格..

回答

3

狀態欄的偏移,在VC的viewDidLoad做到這一點:

if([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) 
    [self setAutomaticallyAdjustsScrollViewInsets:NO]; 

編輯

只是爲了測試,你可以添加4個同樣大小的單元格,並嘗試捕捉到第3和第4個單元格?

修改您scrollViewWillEndDragging執行如下命令:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { 

    NSIndexPath *pathForCenterCell = [self.tableView indexPathForRowAtPoint:CGPointMake(CGRectGetMidX(self.tableView.bounds), CGRectGetMidY(self.tableView.bounds))]; 

    [self.tableView scrollToRowAtIndexPath:pathForCenterCell atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 
} 
+0

加了一個第四,現在它滾動到第三位。從技術上講,這可以解決這個問題,但這真的很糟糕。 –

+0

我編輯了我的答案。請檢查是否適合您。 –

+0

它確實有用!根據速度的方向,我決定改變我的實現,以便它總是「按照」順序依次排列到下一個單元格。但是,在我接受之前,我試圖達到的目標。但是,只有當您的單元格大小接近屏幕大小時纔可以使用。 –