滚动TableView以选中标记tableview单元格

时间:2014-04-11 06:23:14

标签: ios objective-c uitableview

我在tableview中使用触摸事件来选择tableview单元格。我想要tableview慢慢滚动以选择tableview单元格。当我到达框架内的最后一个可见单元格时,我想逐个单元格滚动。我的代码滚动但不准确。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{

    self.scrollEnabled = YES;
 }
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    self.scrollEnabled = YES;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    self.scrollEnabled = NO;
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];
    NSIndexPath *swipedIndexPath = [self indexPathForRowAtPoint:location];
    NSLog(@"%ld",(long)swipedIndexPath.row);
    NSLog(@"%f",location.y);
        NSArray *visibleRows = [self indexPathsForVisibleRows];
        NSIndexPath *firstPath = visibleRows[0];
        NSIndexPath *secondPath = visibleRows[1];
        CGRect firstRowRect = [self rectForRowAtIndexPath:firstPath];
        [self scrollToRowAtIndexPath:(firstRowRect.origin.y > self.contentOffset.y ? firstPath : secondPath) atScrollPosition:UITableViewScrollPositionTop animated:YES];

    UITableViewCell *cell = [self cellForRowAtIndexPath:swipedIndexPath];
    cell.textLabel.text = @"Checked";
    [self reloadRowsAtIndexPaths:@[swipedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}

可以有任何其他选择或我在这里做错了

0 个答案:

没有答案