【发布时间】:2011-09-27 20:44:18
【问题描述】:
我目前正在开发一个 iPhone 应用程序,该应用程序在 UITableView 中使用 UIScrollView 做一些奇怪的事情。这是我第一次涉足 iPhone 开发,所以我敢肯定我错过了一些愚蠢的东西。
在每个 UITableViewCell 中,我都放入了一个基本的 UITableViewCell。在那个 UITableViewCell 是一个标签和一个 UIScrollView。
标签和滚动视图已设置并正常工作,但当它第一次显示时,它在 y 轴上比应有的位置向下偏移了大约 30 个像素,或者由 XIB/NIB 定位。我不会手动移动它。标签显示在正确的位置。在 0,0。 UIScrollView 应该显示在 0,22,但显示更接近 0,40。
当我滑动滚动包含 UITableView 时,假设 UITableView 滚动时 UITableViewCell 离开屏幕,所有 UIScrollViews 都会显示在正确的位置。
这是 UITableView.cellForRowAtIndexPath 的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"GalleryRowCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
[cell.layer setMasksToBounds:TRUE];
[cell.layer setCornerRadius:10.0];
Contagion *c = (Contagion *)[self.dataSet objectAtIndex:indexPath.row];
GalleryRowViewController *subView = [[GalleryRowViewController alloc] initWithContagion:c];
[cell.contentView addSubview:subView.view];
subView.contagionName.text = c.name;
subView.contagion = c;
return cell;
}
这是我的 GalleryRowViewController.viewDidLoad 的代码
- (void)viewDidLoad {
[super viewDidLoad];
self.imageScroll.delegate = self;
[self.imageScroll setBackgroundColor:[UIColor blackColor]];
[self.imageScroll setCanCancelContentTouches:NO];
self.imageScroll.indicatorStyle = UIScrollViewIndicatorStyleWhite;
self.imageScroll.clipsToBounds = NO;
self.imageScroll.scrollEnabled = YES;
self.imageScroll.pagingEnabled = NO;
NSInteger x = 0;
CGFloat xPos = 0;
for (x=0;x<=10;x++) {
UIImage *image = [UIImage imageNamed:@"57-icon.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setBackgroundColor:[UIColor yellowColor]];
CGRect rect = imageView.frame;
rect.size.height = 70;
rect.size.width = image.size.width;
rect.origin.x = xPos;
rect.origin.y = 5;
imageView.frame = rect;
[self.imageScroll addSubview:imageView];
xPos += imageView.frame.size.width+5;
}
[self.imageScroll setContentSize:CGSizeMake(xPos, [self.imageScroll bounds].size.height)];
}
--- 编辑图像---
应用加载后:http://img809.imageshack.us/img809/4576/screenshot20110927at427.png
在屏幕外滚动行后:http://img690.imageshack.us/img690/9461/screenshot20110927at428.png
【问题讨论】:
标签: iphone objective-c xcode uiscrollview