Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UITableViewCell中设置UIScrollView内容偏移量不会';不能在非重用的排队单元中工作_Ios_Objective C_Iphone_Uitableview_Uiscrollview - Fatal编程技术网

Ios 在UITableViewCell中设置UIScrollView内容偏移量不会';不能在非重用的排队单元中工作

Ios 在UITableViewCell中设置UIScrollView内容偏移量不会';不能在非重用的排队单元中工作,ios,objective-c,iphone,uitableview,uiscrollview,Ios,Objective C,Iphone,Uitableview,Uiscrollview,我在我的UITableViewCell子类中放置了一个UIScrollView,我想在UITableView显示它之前,将UIScrollView的内容x偏移量设置为320 所以没问题,我在我的UIScrollView中调用setContentOffset,在configureCell:withindexath:方法中调用后者,并在我的tableView:cellforrowatinexpath:delegate方法中调用后者。这就是它的样子: - (void)viewDidLoad { U

我在我的
UITableViewCell
子类中放置了一个
UIScrollView
,我想在
UITableView
显示它之前,将
UIScrollView
的内容x偏移量设置为320

所以没问题,我在我的
UIScrollView
中调用
setContentOffset
,在
configureCell:withindexath:
方法中调用后者,并在我的
tableView:cellforrowatinexpath:
delegate方法中调用后者。这就是它的样子:

- (void)viewDidLoad {
  UINib *nib = [UINib nibWithNibName:@"myCellNib" bundle:[NSBundle mainBundle]];
  [_tableView registerNib:nib forCellReuseIdentifier:@"cell"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  myCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  [self configureCell:cell atIndexPath:indexPath];
  return cell;
}


- (void)configureCell:(myCell *)cell atIndexPath:(NSIndexPath *)indexPath {
  [cell.scrollView setContentOffset:CGPointMake(320, 0)];
}
到目前为止还不错,除了现在,第一批加载的单元格没有得到期望的结果(除了内容偏移量外,所有子视图及其属性都设置正确),但是如果我向下滚动
UITableView
,新加载(重用)的单元格都很好,我不确定到底发生了什么。停

注意:当我在单元格子视图(
UIScrollView
)上调用
setContentOffset
时,我检查了它们是否为零,并且我从
NSLog(@“%@”,NSStringFromCGPoint(offset))得到了正确的结果

注意:我为此实现的解决方案是检查
UIScrollView
contentSize
属性,并手动设置它是否等于
CGSizeZero
,这是从注册的NIB初始化非重用单元时会发生的情况

if (CGSizeEqualToSize(cell.scrollView.contentSize, CGSizeZero)) {
  [cell.scrollView setContentSize:CGSizeMake(self.view.frame.size.width * 3, 150)];  
}

我认为现在的情况是,您试图在视图生命周期中过早地设置内容偏移量。您需要做的是尝试覆盖
-(void)viewdilayoutsubviews
。也许是这样的:

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];

    //write some loop here to cycle through all the index paths and then:
    myCell *cell = (myCell*)[self.tableView cellForRowAtIndexPath:indexPath];
    [self configureCell:cell atIndexPath:indexPath];



}

编写一个循环,循环遍历所有索引并获取单元格。然后调用configure cell方法。还要确保在prepareCellForReuse中保留用于设置偏移量的代码。这将确保在设置视图之前不会执行此操作,并且它将修复您的问题。这就是为什么重用的单元很好,因为视图已经设置好了。试试这个。

这应该是由
dequeueReusableCellWithIdentifier:forIndexPath:
创建的单元格,当没有可重用的单元格时,但我在阅读::你能发布你的scrollview创建代码吗?我在使用IB。我在代码中用
UITableView
注册了nib文件。这样,
UITableViewCell
就可以使用
initWithCoder:
而不是
initWithStyle:reuseIdentifier:
初始化。是的,试着将代码移动到initWithCoder和prepareforUse。我刚刚尝试过,但并没有预期的效果
prepareforeuse
仅在重用的单元格上调用。他们不是我有问题的人。你能试着把[self-setautomaticallyaadjustsscrollview]放进去吗;在您正在使用的视图控制器或表视图控制器中。在我看来,你似乎不是唯一的一个