看板 MacDev 關於我們 聯絡資訊
※ 引述《y03131003 (Jimmy)》之銘言: : 我有一個tableView : 我在tableView的cell裡裝了一個collectionView : 程式會去網路上抓圖片,抓完之後我想要塞到collectionView的cell裡 : 所以我在抓完圖片的時候 : 呼叫了[self.tableView reloadData] 結果沒反應 : 後來發現應該是要呼叫collectionView的reloadData才對 : 所以我在抓完圖片後呼叫了以下程式 : for (UIView *view in [self.tableView subviews]) { : for (UIView *collection in [view subviews]) { ^^^^^^^^^^^^^^^ 這裡的view你預期應該會抓到cell 但是cell的view hierarchy是這樣的 in iOS6 <UITableViewCell> | <UITableViewCellContentView> | | <custom view objects> in iOS7 <UITableViewCell> | <UITableViewCellScrollView> | | <UITableViewCellContentView> | | | <custom view objects> 要抓到你放在cell裡的collectionView 應該要是要用cell.contentView.subviews contentView property 請看doc: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableViewCell/contentView : if ([collection isKindOfClass:[CollectTableViewCell class]]) { : [[(CollectTableViewCell*)collection collectionView] reloadData]; : } : } : } : (CollectTableViewCell 是裝有collectionView的tableViewCell) : 結果圖片卻沒塞進去,確定有跑到reloadData : 為了debug,我在touch到collectionView的時候呼叫了reloadData : -(void)collectionView:(IndexCollectionView *)collectionView : didSelectItemAtIndexPath:(NSIndexPath *)indexPath : { : [collectionView reloadData]; : } : 結果圖片成功塞進去了! : 有人知道為什麼會這樣嗎? : 我不想要用手點擊才更新collectionView啊QQ 再來,你問說有沒有別的解法 一般來說 既然你的collection view是放在cell裡 那你當然是在更新cell的內容時 一起更新collection view - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ...... [cell.collectionView reloadData] } 你載完圖之後[self.tableView reloadData] UIKit就會call tableView:cellForRowAtIndexPath: [cell.collectionView reloadData]自然也就會被執行了 ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.46.233.9 ※ 編輯: whitefur 來自: 114.46.233.9 (02/08 10:20) ※ 編輯: whitefur 來自: 114.46.233.9 (02/08 10:21)
y03131003:感謝!!我再嘗試看看! 02/08 22:04
howdiun:有觀念有推QAQ 02/11 11:09