【发布时间】:2011-05-18 11:50:29
【问题描述】:
我正在尝试创建与在 AppStore 应用程序中查看屏幕截图完全相同的效果:在 UITableView 中,我有 UITableViewCell 的自定义子类。其中之一旨在显示某些产品的预览,例如 4 张图片。我希望它们以与 AppStore 呈现应用屏幕截图相同的方式显示:在带有附加 UIPageControl 的水平 UIScrollView 内。
所以我将我的 UIScrollView 和 UIPageControl 添加到我的 UITableViewCell 中,就像这样:
@implementation phVolumePreviewCell
- (id)init {
if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kVolumePreviewCellIdentifier]) {
[self setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
[self setSelectionStyle:UITableViewCellSeparatorStyleNone];
previews = [[NSMutableArray alloc] initWithCapacity:4];
for (int i = 0; i < 4; i++) {
[previews addObject:@"dummy"];
}
scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
[scrollView setPagingEnabled:YES];
[scrollView setBackgroundColor:[UIColor grayColor]];
[scrollView setIndicatorStyle:UIScrollViewIndicatorStyleBlack];
[scrollView setShowsHorizontalScrollIndicator:YES];
[scrollView setBounces:YES];
[scrollView setScrollEnabled:YES];
[scrollView setDelegate:self];
[self.contentView addSubview:scrollView];
[scrollView release];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectZero];
[pageControl setNumberOfPages:[previews count]];
[pageControl setBackgroundColor:[UIColor grayColor]];
[self.contentView addSubview:pageControl];
[pageControl release];
}
return self;
}
(注意:我在这里用虚拟数据填充“预览”,只是为了有一个有效的 [预览计数];我想查看 scrollView 的滚动指示器仅用于测试目的,稍后我将隐藏它们开)。
我的问题是我可以滚动包含这个 phVolumePreviewCell(UITableViewCell 的子类)的整个 UITableView,但我不能滚动 UIScrollView。我怎样才能做到这一点?
我找到的唯一信息是:http://theexciter.com/articles/touches-and-uiscrollview-inside-a-uitableview.html 但它谈到了旧的 SDK(即 2.2),我确信有一种更“新”的方法可以做到这一点。
一些精度:
- 我可以用两根手指滚动 UIScrollView。这不是我想要的,我希望它只能用一根手指滚动。
- 当我用两根手指滚动时,TableView 仍会拦截触摸并稍微滚动到顶部或底部。当我触摸 ScrollView 时,我希望 TableView 保持在它的位置。
我相信我必须弄清楚如何拦截 TableView 上的触摸,如果触摸是在 phVolumePreviewCell(自定义 UITableViewCell 子类)上,请将其传递给单元格而不是在 TableView 上处理它。
作为记录,我的 TableView 是在 UITableViewController 子类中以编程方式创建的:
@interface phVolumeController : UITableViewController <UITableViewDelegate> {
LocalLibrary *lib;
NSString *volumeID;
LLVolume *volume;
}
- (id)initWithLibrary:(LocalLibrary *)newLib andVolumeID:(NSString *)newVolumeID;
@end
@implementation phVolumeController
#pragma mark -
#pragma mark Initialization
- (id)initWithLibrary:(LocalLibrary *)newLib andVolumeID:(NSString *)newVolumeID {
if (self = [super initWithStyle:UITableViewStylePlain]) {
lib = [newLib retain];
volumeID = newVolumeID;
volume = [(LLVolume *)[LLVolume alloc] initFromLibrary:lib forID:volumeID];
[[self navigationItem] setTitle:[volume title]];
}
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kVolumePreviewCellIdentifier];
if (cell == nil) {
cell = [[[phVolumePreviewCell alloc] init] autorelease];
}
[(phVolumePreviewCell *)cell setVolume:volume];
return (UITableViewCell *)cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return kVolumePreviewCellHeight;
}
感谢您的帮助!
【问题讨论】:
标签: iphone uitableview uiscrollview scroll