简体   繁体   English

防止所选单元被重用

[英]Prevent selected cell from being reused

So I have a tableview that has a list of items in each cell. 因此,我有一个tableview,其中包含每个单元格中的项目的列表。 Each of these cells contain an image view which, upon being tapped, expands the cell and displays the image for that item. 这些单元格中的每个单元格都包含一个图像视图,点击该图像视图可展开该单元格并显示该项目的图像。 When I scroll down the table view and scroll back up to the cell that was selected, the image is gone. 当我向下滚动表格视图并向上滚动回到选定的单元格时,图像消失了。 I know this is due to reusing cells but I'm not sure on how to keep the expanded cells image in place while scrolling through other items. 我知道这是由于重复使用单元格引起的,但是我不确定在滚动其他项目时如何保持展开的单元格图像在适当的位置。

The closest I've come is here: my table view reuse the selected cells when scroll -- in SWIFT 我最接近的是: SWIFT中滚动时,我的表格视图重用了选定的单元格

If someone could lend me a hand that would be awesome. 如果有人可以帮我,那将是很棒的。 Thanks! 谢谢!

Edit: Adding code snippets - Sorry for the wait. 编辑:添加代码段-对不起,等待。

fileprivate var expandedRowIndex: Int?

// cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    // CatalogItem row.
    let item = self.items[indexPath.row]
    let expanded = indexPath.row == self.expandedItemRowIndex

    // Return standard catalog item cell.
    let reuseID = expanded
                ? CatalogItemCell.PROTOCELL_EXPANDED.id
                : CatalogItemCell.PROTOCELL.id
    let cell = tableView.dequeueReusableCell(withIdentifier: reuseID) as! CatalogItemCell

    // Reset thumbnail image back to nil. Needed so that images appear
    // only in the cell that they belong in.
    if indexPath.row == self.expandedRowIndex{
        cell.uiImage_Thumbnail.image = nil
    }       
    return cell
}

// didSelectRowAt
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    tableView.deselectRow(at: indexPath as IndexPath, animated: true)

    // Expand row - Get the current cell and show image
    self.expandedItemRowIndex = indexPath.row
    let item = self.items[indexPath.row]
    let currentCell = tableView.cellForRow(at: indexPath)

    // Pass both the selected cell and item to the ImageManager
    ImageManager.startImageRequest(currentCell: currentCell!, item: item)

    if self.expandedRowIndex == indexPath.row
    {
       // Selected row is already expanded.
       return
    }

    var reloadPaths = [IndexPath]()

    // Collapse previously expanded row.
    if let previousRowIndex = self.expandedRowIndex
    {
        reloadPaths.append(IndexPath(row: previousRowIndex, section: 0))
    }

    // Expand the selected row.
    self.expandedRowIndex = indexPath.row
    let item = self.items[indexPath.row]
    debugPrint(item.description)
    reloadPaths.append(IndexPath(row: indexPath.row, section: 0))
    tableView.reloadRows(at: reloadPaths as [IndexPath], with: .fade)

}

You can maintain a selectedIndex variable. 您可以维护selectedIndex变量。 In your cellForRow you check whether this call is for selectedCell. 在您的cellForRow中,检查此调用是否针对selectedCell。 If yes, then do the customisation that is required for selected cell. 如果是,则进行所选单元格所需的自定义。

Also you might want to handle heightForRow, there also check whether the call is for selected cell. 另外,您可能要处理heightForRow,还应检查该调用是否针对选定的单元格。

You can maintain an indexPath for selected cell. 您可以为所选单元格维护一个indexPath。 If there are multiple sections. 如果有多个部分。 No need to prevent it from getting reused. 无需阻止其重复使用。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM