0
我有UITableViewCell子視圖作爲UIScrollview和UIscrollview動態uilabels,我需要水平滾動分頁。但我需要同步滾動所有表格視圖單元格。問題是無法滾動所有單元格togeather。如何將UITableviewcell子視圖添加爲uiscrollview
這是我的源代碼。
Customcell來源:
- (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的委託方法:
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (kpiScrollView == self.kpiTableView) {
return;
}
CGPoint contentOffset = kpiScrollView.contentOffset;
for (PageCell *cell in [self.kpiTableView visibleCells]) {
cell.kpiScrollView.contentOffset = contentOffset;
}
}
UITableView是UIScrollView的子類。所以沒有必要添加這個 – Buntylm
@BuntyMadan,這是真的,但我認爲這對他/她沒有幫助。如果我正確地閱讀了這個問題,他們需要在每個單元格內的滾動視圖**,這允許他們在每個單元格內水平滾動,以及與表格視圖垂直滾動。溫度,如果我錯了,請糾正我。 – Nate
同意@Nate。我看到你在這裏想要做什麼,但是在代碼中有一些真正奇怪的東西:例如cell.self.kpiScrollView.delegate = self;如果(kpiScrollView == self.kpiTableView)......當然這些不會是平等的。 – danh