gpt4 book ai didi

ios - 如何将 UITableviewcell subview 添加为 uiscrollview

转载 作者:行者123 更新时间:2023-11-29 03:59:54 25 4
gpt4 key购买 nike

我有 UITableViewCell subview 作为 UIScrollview 和 UIscrollview 作为动态 uilabels,我需要通过分页水平滚动。但我需要同步滚动所有 TableView 单元格。问题是无法将所有单元格滚动到一起。

这是我的源代码。

自定义单元来源:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code

//ScrollView
self.kpiScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 300, 70)];
[self.kpiScrollView setPagingEnabled:YES];
[self.contentView addSubview:self.kpiScrollView];
[self.kpiScrollView release];

NSArray *colors = [NSArray arrayWithObjects:[UIColor grayColor], [UIColor greenColor], [UIColor blueColor], nil];

for (int i =0; i<colors.count; i++) {
CGRect frame;
frame.origin.x = self.kpiScrollView.frame.size.width *i;
frame.origin.y = 0;
frame.size = self.kpiScrollView.frame.size;

subView = [[UIView alloc] initWithFrame:frame];
subView.backgroundColor = [colors objectAtIndex:i];
[self.kpiScrollView addSubview:subView];
[subView release];
}
self.kpiScrollView.contentSize =
CGSizeMake(self.kpiScrollView.frame.size.width*colors.count,
self.kpiScrollView.frame.size.height);
}
}

和TableView源代码:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"CellIdentifier";
PageCell *cell = (PageCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[PageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];
}
// cell.pageDelegate = self;
cell.self.kpiScrollView.delegate= self;
cell.tag = indexPath.row+1;
NSLog(@"cell tag:%d", cell.tag);

return cell;
}

UIScrollView 委托(delegate)方法:

- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (kpiScrollView == self.kpiTableView) {
return;
}
CGPoint contentOffset = kpiScrollView.contentOffset;
for (PageCell *cell in [self.kpiTableView visibleCells]) {
cell.kpiScrollView.contentOffset = contentOffset;
}
}

最佳答案

浏览一下代码,不确定是否还有其他问题,但首先要修复的是在哪里获取内容偏移量......

- (void)scrollViewDidScroll:(UIScrollView *)sender {

// note the change here...
if (sender == self.kpiTableView) return;

// get the content offset from the cell's scrollview that posted this delegate message
CGPoint contentOffset = sender.contentOffset;
for (PageCell *cell in [self.kpiTableView visibleCells]) {
cell.kpiScrollView.contentOffset = contentOffset;
}
}

关于ios - 如何将 UITableviewcell subview 添加为 uiscrollview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16008134/

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