1

我有UI,如附图所示。它有两个组件,主要是UICollectionView和UITableView。UICollectionView更新特定单元格的单元格内容

enter image description here

我试图当用户从任何的UITableView细胞在这里实现,我想更新顶部CollectionViewCell。

我到目前为止做了什么。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

if(![self.selectedIndexPath isEqual:indexPath]){ 
    UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.selectedIndexPath]; 
    uncheckCell.accessoryType = UITableViewCellAccessoryNone; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

} 

UITableViewCell* cellCheck = [tableView cellForRowAtIndexPath:indexPath]; 
cellCheck.accessoryType = UITableViewCellAccessoryCheckmark; 
self.selectedIndexPath = indexPath; 

NSIndexPath *collectionIndex = [NSIndexPath indexPathForItem:0 inSection:self.currentPage]; 
//TODO:Find Correct Cell 
UICollectionViewCell *cellToUpdate = [self.collectionView cellForItemAtIndexPath:collectionIndex]; 

for (UIView *subView in [cellToUpdate.contentView subviews]) { 
    if ([subView isKindOfClass:[CustomView class]]) { 
     CustomView *infoView = (CustomView *)subView; 
     [infoView updateInfoWithIndex:indexPath.row]; 
    } 
} 
[self.collectionView reloadData]; 
} 

我觉得我在做下面这行错了。不知道是什么?

NSIndexPath *collectionIndex = [NSIndexPath indexPathForItem:0 inSection:self.currentPage]; 
+0

使用委托伴侣。 –

回答

0

您应该使用reloadItems(at indexPaths: [IndexPath]),只是通过你感兴趣的索引路径。然后,只需做图中的UICollectionViewDataSource方法。

0

我不能向你展示Obj-C的代码,我没有永远触摸过它。但是对于你的问题的答案应该相当简单。你需要使用代表。

基本上,这个想法是如下:“被选中的东西”

当您点击从tableview中的任意单元格,你火,说的事件。

从视角或你的tableview来看,这就是知道的一切。你不能让桌面视图知道上面有一个collectionview,当然不是从外面看它的单元格的内容。

单元必须从内部更新自己。这是关键。

所以,你要做的就是,当你创建这些细胞,他们订阅的事件(手段,他们听的话),而当它被解雇,他们会从里面做出相应的反应。

由于只有一个细胞可见,只有一个应该订阅,它很容易做到的细胞,你只要在订阅和cellforrow将作出反应,细胞永远是正确的。如果不是,你将不得不使用索引路径。

请注意,我不确定是否应该取消订阅这些事件(否则所有先前创建的单元格可能会留在内存中,或者甚至仍然对事件做出响应),因为我不记得这是如何工作的在obj-c中。我猜应该有一个只接受一个用户的事件类型,所以如果可以的话就使用它。

如果您的单元以某种方式需要来自外部的数据,则可以在cellSelected中将参数传递给事件之前将其传递给该事件。

一旦你把所有这些设置好了,你就是金子。

0

它取决于你的实现,你是如何将你的单元格分为多个部分的,但是我们假设你有一个部分,数组中的每个元素都对应于你的表中的一行。

如果你想重新加载相应的数组元素的索引x小区内所有你需要做的就是写

[collectionView performBatchUpdates:^{ 
collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:x section:0]];}]; 

相反,如果你有电池,但要找到它的indexpath您可以随时拨打[collectionView indexPathForCell:myCell]

1

试试这个代码,我做的演示,为您和其工作100%

@IBOutlet var tblview: UITableView! 
    let arrm :NSMutableArray = NSMutableArray() 
    @IBOutlet var collectionview: UICollectionView! 
    var arrayofdata = [ 
    ["image":"2.jpg","title":"Himanshu"], 
    ["image":"1.png","title":"Himanshu"], 
    ["image":"2.jpg","title":"Himanshu"], 
    ["image":"2.jpg","title":"Himanshu"], 
    ["image":"2.jpg","title":"Himanshu"], 
    ["image":"1.png","title":"Himanshu"], 
    ["image":"2.jpg","title":"Himanshu"], 
    ["image":"1.png","title":"Himanshu"], 
    ["image":"2.jpg","title":"Himanshu"], 
    ["image":"2.jpg","title":"Himanshu"], 
    ["image":"2.jpg","title":"Himanshu"],] 

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return arrayofdata.count; 
    } 
    // make a cell for each cell index path 
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("InviteFriendCell", forIndexPath: indexPath) 

     profileimage.image = UIImage(named:arrayofdata[indexPath.row]["image"]!) 
    profileimage.layer.masksToBounds=false 
    if arrm.containsObject("text\(indexPath.row)") { 
     profileimage.layer.cornerRadius = profileimage.frame.size.height/2 
     profileimage.layer.masksToBounds=true 
    } 

     return cell 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 5 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell") 
     cell?.textLabel?.text = "text\(indexPath.row)" 
     return cell! 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     if arrm.containsObject("text\(indexPath.row)") { 

     }else 
     { 
      arrm.addObject("text\(indexPath.row)") 
     } 
     collectionview.reloadData() 
    } 
相关问题