我正在尝试创建与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;
}
谢谢你的帮助!