【问题标题】:Is that possible to use UIPageControl to control the move of UITableView?是否可以使用 UIPageControl 来控制 UITableView 的移动?
【发布时间】:2011-08-24 17:23:14
【问题描述】:

从 Apple 示例“PageControl”中我们可以知道 UIPageControl 可用于控制页面在滚动视图中的移动。

由于 UITableView 是 UIScrollView 的子类,我想使用 UIPageControl 来控制表格视图单元格的移动,就像在“PageControl”中一样。

但是找不到任何委托接口让我这样做。

问题是:

可以这样做吗?为什么?

谢谢

让我回答我的问题:

以编程方式选择和滚动

清单 6-5 以编程方式选择行

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath {
    NSIndexPath *scrollIndexPath;
    if (newIndexPath.row + 20 < [timeZoneNames count]) {
        scrollIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row+20 inSection:newIndexPath.section];
    } else {
        scrollIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row-20 inSection:newIndexPath.section];
    }
    [theTableView selectRowAtIndexPath:scrollIndexPath animated:YES
                        scrollPosition:UITableViewScrollPositionMiddle];
}

【问题讨论】:

    标签: ios uitableview ios4 uiscrollview uipagecontrol


    【解决方案1】:

    这当然是可能的,但你为什么想要这个?表格视图垂直滚动,而页面控件具有水平布局,因此它可能看起来/感觉很奇怪。

    无论如何,如果您真的想这样做,请将您的视图控制器添加为页面控件的目标:

    [pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
    

    然后在action中实现滚动:

    - (void)pageChanged:(UIPageControl *)sender {
        float scrollPosition = ((float)sender.currentPage / (float)sender.numberOfPages) * tableView.contentSize.height;
        [tableView scrollRectToVisible:CGRectMake(0, scrollPosition, 1, 1) animated:YES];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-05
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      相关资源
      最近更新 更多