Я создал tableView с пользовательскими ячейками, которые содержат изображение и scrollView. Все виды прокрутки содержат метки более широкие, чем границы экрана, поэтому, когда я просматриваю влево/вправо, я хочу, чтобы все scrollViews прокручивались в том же направлении. Проблема в том, что я не знаю, как получить ссылку на каждый scrollView в методе scrollViewDidScroll.scrollViewDidScroll метод, не ссылающийся на все scrollviews из вида таблицы
мой ViewController класс:
#import "EPGViewController.h"
@interface EPGViewController(){
NSArray *EPGList;
int scrollPositionX;
}
@end
@implementation EPGViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBarHidden = false;
EPGList = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12", nil];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return EPGList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *hlCellID = @"EPGCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:hlCellID];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:hlCellID];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
UILabel *label = (UILabel *)[cell viewWithTag:15];
label.text = EPGList[indexPath.row];
UIScrollView *scrolView = (UIScrollView *)[cell viewWithTag:16];
scrolView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
scrolView.delegate = self;
scrolView.tag = indexPath.row+101;
scrolView.scrollEnabled = YES;
scrolView.contentSize = CGSizeMake(scrolView.frame.size.width,scrolView.frame.size.height);
[scrolView setShowsHorizontalScrollIndicator:NO];
[scrolView setShowsVerticalScrollIndicator:NO];
int i=0;
for(i=0;i<15;i++){
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0+i*100, 0, 100, 100)];
label.text = @"HELLO";
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont fontWithName:@"ArialMT" size:18];
[scrolView addSubview:label];
scrolView.contentSize = CGSizeMake(scrolView.frame.size.width+i*label.frame.size.width,scrolView.frame.size.height);
}
[cell addSubview:scrolView];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 80.0;
}
- (void)scrollViewDidScroll:(UIScrollView *)callerScrollView {
scrollPositionX = callerScrollView.contentOffset.x;
//if this is called with table view exit
if (callerScrollView == self.tableView) return;
int indexPath = callerScrollView.tag-101;
NSLog(@"TAG: %d",indexPath);
for (UITableViewCell *cell in self.tableView.visibleCells) {
//TODO: Don’t use tags.
UIScrollView *cellScrollView = (UIScrollView *)[cell viewWithTag:16];
if (callerScrollView == cellScrollView) continue;
cellScrollView.contentOffset = CGPointMake(scrollPositionX, 0);
}
}
@end
EDIT:
мне удалось решить мою проблему, удалив строку, в которой я добавить различные теги к scrollviews в виде таблицы. теперь я просто устанавливаю смещение на scrollview с тегом 16.
В каком классе вы указываете scrollViewDidScroll? –
В viewController, который контролирует представление таблицы. UIScrollViewDelegate включен в файл .h. – lugonja