注意:_selectionRect是在init方法中写死的
eg:_selectionRect = CGRectMake(0,self.frame.size.height/2-22.5,45.0, 45.0);
如何让tableview滚动到点击的cell的位置
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView ==_tableView) {
selectedIndexPath = indexPath;
//滚动到点击的那一个cell四种选择
//UITableViewScrollPositionBottom UITableViewScrollPositionNone UITableViewScrollPositionTop UITableViewScrollPositionMiddle
[_tableViewscrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionBottomanimated:YES];
//通知vc点击了哪一个cell
[self.delegateselector:_tableViewdidSelectRowAtIndex:indexPath.row];
[_tableViewreloadData];
}
}
#pragma mark拖动的时候
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (!decelerate) {
[selfscrollToTheSelectedCell];
}
}
#pragma mark 拖动执行的关键代码
- (void)scrollToTheSelectedCell {
CGRect selectionRectConverted = [selfconvertRect:_selectionRecttoView:_tableView];
NSArray *indexPathArray = [_tableViewindexPathsForRowsInRect:selectionRectConverted];
CGFloat intersectionHeight =0.0;
for (NSIndexPath *indexin indexPathArray) {
//looping through the closest cells to get the closest one
UITableViewCell *cell = [_tableViewcellForRowAtIndexPath:index];
CGRect intersectedRect =CGRectIntersection(cell.frame, selectionRectConverted);
if (intersectedRect.size.height>=intersectionHeight) {
selectedIndexPath = index;
intersectionHeight = intersectedRect.size.height;
}
}
if (selectedIndexPath!=nil) {
//As soon as we elected an indexpath we just have to scroll to it
[_tableViewscrollToRowAtIndexPath:selectedIndexPathatScrollPosition:UITableViewScrollPositionBottomanimated:YES];
[self.delegateselector:_tableViewdidSelectRowAtIndex:selectedIndexPath.row];
[_tableViewreloadData];
}
}