gpt4 book ai didi

iPhone - Objective C - UITableView 内的 UIScrollView 渲染有趣

转载 作者:行者123 更新时间:2023-12-03 21:18:29 26 4
gpt4 key购买 nike

我目前正在开发一个 iPhone 应用程序,该应用程序在 UITableView 中使用 UIScrollView 做一些奇怪的事情。这是我第一次涉足 iPhone 开发,所以我确信我错过了一些愚蠢的东西。

在每个 UITableViewCell 中,我放入一个基本的 UITableViewCell。其中 UITableViewCell 是一个 Label 和一个 UIScrollView。

标签和 ScrollView 已设置并正常工作,但当它首次显示时,它在 y 轴上比应有的位置向下偏移了约 30 像素,或者由 XIB/NIB 定位。我不会手动移动它。标签显示在正确的位置。在 0,0。 UIScrollView 应显示在 0,22,但显示更接近 0,40。

当我滑动滚动包含的 UITableView 时,所有 UIScrollView 将显示在正确的位置,假设当 UITableView 滚动时 UITableViewCell 离开屏幕。

这是 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

最佳答案

好吧,由于我之前的回答水平太低,所以让我再尝试一下。

首先,我刚刚注意到核心问题,即您为每个单元格使用 View Controller 。引用 Apple 的话,““单个 View Controller 通常管理与单个屏幕的内容相关的 View 。”这也将摆脱你的 XIB(只需手动配置你的 ScrollView ),我打赌这将解决你的问题。

要继续,您的主要选择是是否按照 Scott 的建议创建 ContagionTableViewCell 类。

如果是这样,请按照 Elements 示例,创建 UITableViewCell ContagionTableViewCell 的子类,并具有 ScrollView 、标签 View 和传染性属性。就像他们为元素使用自定义 setter 一样,为传染使用一个自定义 setter ,这样每当分配它时,它也会更新单元格标签(和关联的图片)。

将 imageScroll 代码从 GalleryRowViewController.viewDidLoad 移至 ContagionTableViewCell 初始化代码中。将图像代码放入一个新例程中,该例程将从传染 setter 中调用。

如果不是,则将 GalleryRowView Controller 代码移至 UITableView 中。我建议你看一下 Apple 的 tableViewSuite 中的 cellForRowAtIndexPath,这是关于 subview 的第四个示例。特别是,它显示了这种将单元的创建(当您需要全新的单元时)与配置单元(当重用它时)分开的模式。由于你的scrollView中有10个imageView,你必须决定是否删除所有这些(和/或scrollview),或者只是在重复使用单元格时进入内部并更新它们的图像。

关于iPhone - Objective C - UITableView 内的 UIScrollView 渲染有趣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7575419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com