2014-02-13 40 views
2

我有動態TableView,原型單元格中有CollectionView。我創建了UITableViewCell的子類併爲TableView原型單元添加了Custom單元。此外,我還將UICollectionVeiwCell添加爲CollectionView的CustomCell。動態TableView與原型單元格中的CollectionView

它是什麼樣子的故事板: enter image description here

代碼下面,我使用的是創建我的場景:

//-=-=-=-==-=-=--==-=-=-=-=-=-=-=-=--=-=-=-TableView methods-=-=-=-=--=-=-=-=-=-=-=-= 
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 15; 
} 

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

     myCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

    if (!cell) { 
      cell = [[myCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
    } 
     return cell; 
} 

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-==-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-== 


//-=-=-=-==-=-=--==-=-=-=-=-=-=-=-=--=-=-=-CollectionView methods-=-=-=-=--=-=-=-=-=-=-=-= 
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 
    return 1; 
} 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    return 7; 
} 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

     static NSString* cellIdentifier = @"CVCell"; 
     CVCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 

    if (indexPath.row == 0) { 
     cell.cellTxtFld.text =[NSString stringWithFormat:@"%ld", (long)indexPath.row]; 
    } 

    if (indexPath.row == 1) { 
     cell.cellTxtFld.text =[NSString stringWithFormat:@"%ld", (long)indexPath.row]; 
    } 

    if (indexPath.row == 2) { 
     cell.cellTxtFld.text =[NSString stringWithFormat:@"%ld", (long)indexPath.row]; 
    } 

    if (indexPath.row == 3) { 
     cell.cellTxtFld.text =[NSString stringWithFormat:@"%ld", (long)indexPath.row]; 
    } 

    if (indexPath.row == 4) { 
     cell.cellTxtFld.text =[NSString stringWithFormat:@"%ld", (long)indexPath.row]; 

    } 

    if (indexPath.row == 5) { 
     cell.cellTxtFld.text =[NSString stringWithFormat:@"%ld", (long)indexPath.row]; 
    } 

    if (indexPath.row == 6) { 
     cell.cellTxtFld.text [email protected]"LAST"; 
    } 

     return cell; 
    } 

它是如何看起來像在模擬器:

enter image description here

我的問題是,我怎樣才能直接訪問每個collectionView?例如,我有15個數組,我希望第一個collectionView(在第一個TableView的行中)由0-index,second-1索引等等初始化。我怎樣才能做到這一點?

回答

0

您可以將collectionView作爲Table視圖的自定義視圖類中的屬性並在其後處理。將數組從自定義單元格中的方法傳遞給自定義單元格的數組。然後您將能夠直接訪問每個tableView的集合視圖。

編輯:更具體:

你有一個的tableView細胞myCustomCell。在其中創建UICollectionView的屬性,並且可以將其委託僅設置爲控制器。在myCustomCell中製作一個方法。可以在cellForAtIndexpath中調用,並將代理設置爲self。現在,您可以根據您的需要訪問您的UICollectionView

+0

請問您能更具體嗎? – daleijn

+0

檢查我的編輯... –

+0

您的建議導致編譯失敗。我通過簡單和愚蠢的方式 - 在CellForItemForIndexPath中使用計數器。但我不知道我可以通過我的方式爲某些TextField獲取textField.text。你可以幫我嗎? – daleijn

相關問題