2014-09-02 64 views
0

我在組(分區)模式下有UITableView。該表的每個部分都包含約5個單元格。 表中的每個單元由一個UILabel和一個UICollectionView組成。 UICollectionView將包含一組具有水平滾動的簡單項目。UICollectionView裏面的UITableView不正確的行

所以,我有類似網格視圖,它看起來在故事板,如: enter image description here

在它看起來像模擬器: enter image description here

的問題,我可以」我知道:

當我滾動任何行水平 - 一些另一行(可能是在同一個或另一個部分)可以與第一個同步滾動,看起來他們之間有一些連接,或者它是相同的UICollectionView

如何在每個UITableViewCell中使用不同的UICollectionView或打破「奇怪的連接」?

我的代碼(報頭):

@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 

實現:

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

所有的初始工作(如ID的登記,細胞原型等)使用故事板完成的。

+0

這個問題可能是findParentTableCell,但我看不到代碼。遍歷視圖層次結構總是有風險的,因爲事情可能會在版本之間發生變化,並且它不是很好的設計。如果我是你,我將創建一個數據模型類,並使該類實現集合視圖委託,然後在cellForRowAtIndexPath中,您可以簡單地將單元的集合視圖的委託分配給適當的數據模型對象 – Paulw11 2014-09-02 22:21:52

回答

0

你的問題可能與UITableViewCells被重用的事實有關。所以當你滾動並看到一個新的單元格時,它實際上只是一個消失的單元格。爲了處理你的問題,你需要確保cellForRowAtIndexPath中的單元格被正確初始化,並確保你覆蓋了你之前的任何數據。

+0

是的,看起來你是對的,但我不知道如何正確「重置」UICollecionView狀態。例如,如何停止它的滾動(當我滾動A-TableViewCell的UICollectionView,並且它正在移動時,我真的可以看到一些C-TableViewCell也在移動),等等...... – Zarathustra 2014-09-03 07:43:04

+1

你可以嘗試調用'[[cell excerciseTrials] setContentOffset:CGPointZero animated:NO]'調用重載數據後。 – Rob 2014-09-03 19:26:19

+1

問題在於,無論何時滾動,收藏視圖的滾動將始終重置爲開始。爲避免這種情況,您可以跟蹤每個集合視圖的當前內容偏移量,並將其設置爲cellForRowAtIndexPath而不是CGPointZero。 – Rob 2014-09-03 19:30:01

-1

好吧,最後我決定通過創建獨特的Cell ID並在沒有Storyboard原型的情況下「即時」創建它,從而避免重複使用功能。現在看起來很好。

的代碼標準:

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