Ios UICollectionView内部UITableView节中的行不正确
我在组分段模式下有一个UITableView。表的每个部分包含大约5个单元格。 表的每个单元格由一个UILabel和一个UICollectionView组成。UICollectionView将包含一组带有水平滚动的简单项 所以,我有一个类似网格视图的东西,它在故事板中看起来像: 在模拟器中,它看起来像: 我无法理解的问题是: 当我水平滚动任何一行时-可能在同一行中的另一行或另一节中的另一行可以与第一行同步滚动,看起来它们彼此有一些连接,或者是相同的UICollectionView 如何在每个UITableViewCell中使用不同的UICollectionView或断开这种奇怪的连接 我的代码头:Ios UICollectionView内部UITableView节中的行不正确,ios,objective-c,uitableview,uicollectionview,Ios,Objective C,Uitableview,Uicollectionview,我在组分段模式下有一个UITableView。表的每个部分包含大约5个单元格。 表的每个单元格由一个UILabel和一个UICollectionView组成。UICollectionView将包含一组带有水平滚动的简单项 所以,我有一个类似网格视图的东西,它在故事板中看起来像: 在模拟器中,它看起来像: 我无法理解的问题是: 当我水平滚动任何一行时-可能在同一行中的另一行或另一节中的另一行可以与第一行同步滚动,看起来它们彼此有一些连接,或者是相同的UICollectionView 如何在每个
所有初始工作(如ID注册、单元原型制作等)都是使用故事板完成的。您的问题可能与UITableViewCells被重用有关。因此,当你滚动并看到一个新的单元格时,它实际上只是消失的单元格之一。为了解决您的问题,您需要确保单元在CellForRowatineXpath中正确初始化,并确保覆盖其中的任何以前的数据。好的,最后我决定通过创建唯一的单元ID并在不使用故事板原型的情况下动态创建它来避免单元重用功能。现在看起来很好 代码是标准的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellId = [NSString stringWithFormat:@"%@-%ld-%ld",
athleteHistoryTable_ExcerciseTrialsCell,
(long)[indexPath section],
(long)[indexPath row]];
ipAthleteHistoryTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil)
{
cell = [[ipAthleteHistoryTableCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellId];
[[cell excerciseTrials] registerClass:[ipAthleteHistoryExcerciseTrialCell class]
forCellWithReuseIdentifier:athleteHistoryTable_TrialCell];
[[cell excerciseTrials] setDataSource:self];
}
[[cell excerciseTitle] setText:[NSString stringWithFormat:@"Excercise Title %li:%li; %ld",
[indexPath section],
(long)[indexPath row],
(long)[cell excerciseTrials]]];
[[cell excerciseTrials] reloadData];
return cell;
}
问题可能在findParentTableCell中,但我看不到该代码。遍历视图层次结构总是有风险的,因为在不同版本之间可能会发生变化,这不是很好的设计。如果我是你,我会创建一个数据模型类,让这个类实现集合视图委托,然后在CellForRowatineXpath中,你可以简单地将单元格集合视图的委托分配给相应的数据模型对象是的,看起来你是对的,但我不确定如何正确地重置UICollecionView状态。例如,当我滚动A-TableViewCell的UICollectionView时,如何停止滚动,当它移动时,我真的可以看到一些C-TableViewCell也在移动,等等…在调用重新加载数据后,您可以尝试调用[[cell ExerciseTrials]setContentOffset:CGPointZero animated:NO],集合视图的滚动将始终重置为开头。为了避免这种情况,您可以跟踪每个集合视图的当前内容偏移量,并将其设置为cellForRowAtIndexPath中的CGPointZero。
- (void)viewDidLoad
{
_dumbNumberOfItems = 24;
_dumbNumberOfItems2 = 5;
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dumbNumberOfItems2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _dumbNumberOfItems;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"12.09.2014:%ld", (long)section];
}
- (UITableView *)findParentTableView:(UITableViewCell *)tableCellView
{
return (UITableView *)[ipNavigationHelper findParentViewOfClass:[UITableView class]
ofChildView:tableCellView];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ipAthleteHistoryTableCell *cell = [tableView dequeueReusableCellWithIdentifier:athleteHistoryChart_ExcerciseTrialsCell forIndexPath:indexPath];
[[cell excerciseTitle] setText:[NSString stringWithFormat:@"Title %li:%li; %ld", [indexPath section], (long)[indexPath row], (long)[cell excerciseTrials]]];
[[cell excerciseTrials] reloadData];
return cell;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ipAthleteHistoryExcerciseTrialCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:athleteHistoryChart_TrialCell
forIndexPath:indexPath];
ipAthleteHistoryTableCell *upCell = [self findParentTableCell:collectionView];
[[cell actualTrial] setText:[ipNumeralsConverter convertToLatinArabNumber:[indexPath row]]];
[[cell actualRepeats] setText:[NSString stringWithFormat:@"%li", [[[self findParentTableView:upCell] indexPathForCell:upCell] section]]];
[[cell targetRepeats] setText:[NSString stringWithFormat:@"%li", [[[self findParentTableView:upCell] indexPathForCell:upCell] row]]];
[[cell workWeight] setText:[NSString stringWithFormat:@"%li", [indexPath row]*10]];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellId = [NSString stringWithFormat:@"%@-%ld-%ld",
athleteHistoryTable_ExcerciseTrialsCell,
(long)[indexPath section],
(long)[indexPath row]];
ipAthleteHistoryTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil)
{
cell = [[ipAthleteHistoryTableCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellId];
[[cell excerciseTrials] registerClass:[ipAthleteHistoryExcerciseTrialCell class]
forCellWithReuseIdentifier:athleteHistoryTable_TrialCell];
[[cell excerciseTrials] setDataSource:self];
}
[[cell excerciseTitle] setText:[NSString stringWithFormat:@"Excercise Title %li:%li; %ld",
[indexPath section],
(long)[indexPath row],
(long)[cell excerciseTrials]]];
[[cell excerciseTrials] reloadData];
return cell;
}