【问题标题】:UICollectionView Update Cell content for specific cellUICollectionView 更新特定单元格的单元格内容
【发布时间】:2016-10-06 06:46:48
【问题描述】:

我有如附图所示的用户界面。它有两个组件,主要是 UICollectionView 和 UITableView。

当用户从 UITableView 中选择任何单元格时,我想在这里实现什么,我想更新 Top 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];

【问题讨论】:

  • 使用代理伙伴。

标签: ios objective-c uicollectionview uicollectionviewcell nsindexpath


【解决方案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()
    }

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      基本上,想法如下:

      当您从 tableview 中点击任何单元格时,您会触发一个事件,显示“选择了某些内容!”。

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

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

      所以你要做的是,当你创建这些单元格时,它们会订阅那个事件(意味着,它们会监听它),当它被触发时,它们会从内部做出相应的反应。

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

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

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

      一旦你设置了所有这些,你就是黄金。

      【讨论】:

        【解决方案4】:

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

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

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

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

        【讨论】:

          猜你喜欢
          • 2013-12-16
          • 2018-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多