UICollectionView更新特定单元格的单元格内容

时间:2016-10-06 06:46:48

标签: ios objective-c uicollectionview uicollectionviewcell nsindexpath

我有UI,如附图所示。它有两个组件,主要是UICollectionView和UITableView。

enter image description here

当用户从UITableView中选择任何单元格时,我想要实现的目标,我想更新Top CollectionViewCell。

到目前为止我做了什么。

function playAudio{
self.songChanged = false;
      setTimeout(function() {
        self.songChanged = true;
        $scope.$apply();
      }, 10);
  
  }

我认为我在接下来的行中做错了。不确定是什么?

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

4 个答案:

答案 0 :(得分: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()
    }

答案 1 :(得分:0)

您应该使用reloadItems(at indexPaths: [IndexPath])并只传递您感兴趣的索引路径。然后只需使用UICollectionViewDataSource方法进行绘制。

答案 2 :(得分:0)

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

基本上,这个想法如下:

当你点击tableview中的任何单元格时,你会触发一个“选择了某个东西!”的事件。

从观点或您的桌面视图来看,这是应该知道的一切。你不能让tableview知道上面有一个集合视图,当然也不是外面的单元格内容。

细胞必须从内部更新自己。这是关键。

所以你所做的就是,当你创建那些单元格时,他们会订阅那个事件(意思是,他们会听到它),当它被触发时,它们会从内部作出相应的反应。

由于只有一个单元格可见,并且只有一个单元格应该订阅,因此很容易做到,您只需在cellforrow中订阅,并且将作出反应的单元格将始终正确。如果没有,你将不得不使用索引路径。

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

如果您的单元格需要来自外部的数据,您可以在触发之前将参数传递给事件,在cellSelected中。

一旦你完成所有这些,你就是金。

答案 3 :(得分:0)

这取决于您的实现,如何将单元格分组为多个部分,但假设您有一个部分,并且数组中的每个元素都对应于表格中的一行。

如果你想在索引x重新加载对应于数组元素的单元格,你需要做的就是写

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

相反,如果您有单元格但想要找到其索引路径,则可以随时调用[collectionView indexPathForCell:myCell]

相关问题