简体   繁体   English

UITableView内的UICollectionView部分中的错误行

[英]UICollectionView inside UITableView incorrect rows in sections

I have a UITableView in the Groups (Sectioned) mode. 我在组(分节)模式下有一个UITableView Each section of the table contains about 5 cells. 该表的每个部分包含大约5个单元格。 Each cell of the table consists of one UILabel and one UICollectionView . 该表的每个单元格由一个UILabel和一个UICollectionView UICollectionView will contain a set of simple items with a horizontal scrolling. UICollectionView将包含一组具有水平滚动的简单项目。

So, I have something like a Grid View and it looks in Storyboard like: 所以,我有一个类似于Grid View的东西,它在Storyboard中看起来像: 在此处输入图片说明

In the Simulator it looks like: 在模拟器中,它看起来像: 在此处输入图片说明

The problems that I can't understand: 我无法理解的问题:

When I scroll any of the rows horizontally - some another row (that may be in the same or some another section) can synchronously scrolls with the first one, looks like they have some connection with each other or it's the same UICollectionView ! 当我水平滚动任何行时-另一行(可能在同一节或另一节中)可以与第一行同步滚动,看起来它们彼此之间有某种联系,或者是相同的UICollectionView

How can I use different UICollectionView in each UITableViewCell or break that "strange connection"? 如何在每个UITableViewCell使用不同的UICollectionView或断开该“奇怪的连接”?

My code (Header): 我的代码(标题):

@interface ipAthleteHistoryTableViewController : UIViewController <UICollectionViewDataSource, UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITextField *datesRangeFromPeer;
@property (weak, nonatomic) IBOutlet UITextField *datesRangeToPeer;

@property (weak, nonatomic) IBOutlet UITableView *history;

- (IBAction)datesRangeFromPeerEditingExited:(id)sender;
- (IBAction)datesRangeFromPeerSet:(id)sender;

- (IBAction)datesRangeToPeerEditingExited:(id)sender;
- (IBAction)datesRangeToPeerSet:(id)sender;

- (IBAction)todaySpeedButtonPressed:(id)sender;
- (IBAction)weekSpeedButtonPressed:(id)sender;
- (IBAction)monthSpeedButtonPressed:(id)sender;
- (IBAction)quarterSpeedButtonPressed:(id)sender;

@end

Implementation: 实现方式:

- (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;
}

All the initial work (like IDs registration, cells prototyping, etc.) is done using Storyboard. 所有初始工作(例如ID注册,单元原型制作等)都是使用Storyboard完成的。

Your problem is probably related to the fact that UITableViewCells get reused. 您的问题可能与UITableViewCells被重用有关。 So when you scroll and see a new cell, it really is just one of the cells that went away. 因此,当您滚动并看到一个新的单元格时,它实际上只是消失的单元格之一。 To deal with your issues, you'll need to make sure the cell is properly initialized in cellForRowAtIndexPath and make sure you override any previous data you had in there. 要解决您的问题,您需要确保在cellForRowAtIndexPath中正确初始化了单元格,并确保您覆盖了其中的所有先前数据。

Ok, finally I've decided to escape from the cells reusing functionality by creating unique Cell IDs and creating it "on-the-fly" without Storyboard prototyping. 好的,最后我决定通过创建唯一的单元ID并在没有Storyboard原型的情况下“即时”创建单元来重用单元。 It looks fine now. 现在看起来不错。

The code is standard: 该代码是标准的:

- (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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM