Swift 使单元格在UITableView中可见

Swift 使单元格在UITableView中可见,swift,uitableview,cell,Swift,Uitableview,Cell,][2我有一个包含某些内容的tableView。所有单元格都显示得非常完美。但是删除几行后,下面的某些单元格将不可见。我正在使用方法:tableView删除单元格。deleteRows(位于:[indexPath],使用:.automatic)。但是它们有数据,高度也很好。过了一段时间,我找到了一个解决方案:我在willDisplayCell方法中将isHidden的单元格属性更改为false。我认为这不是一个好的解决方案,但它起了作用。你认为呢?为什么单元格没有出现 func tableVie

][2我有一个包含某些内容的tableView。所有单元格都显示得非常完美。但是删除几行后,下面的某些单元格将不可见。我正在使用方法:
tableView删除单元格。deleteRows(位于:[indexPath],使用:.automatic)
。但是它们有数据,高度也很好。过了一段时间,我找到了一个解决方案:我在
willDisplayCell
方法中将isHidden的单元格属性更改为false。我认为这不是一个好的解决方案,但它起了作用。你认为呢?为什么单元格没有出现

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return myFavoritesProductsDataSource.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "productCell") as! ProductTableViewCell
    cell.product = myFavoritesProductsDataSource[indexPath.row]
    return cell
}

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let delete = UIContextualAction(style: .destructive, title:  "Удалить", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in 
    self.myFavoritesProductDataSource.remove(at: indexPath.row)
        tableView.beginUpdates()
        tableView.deleteRows(at: [indexPath], with: .automatic)
        tableView.endUpdates()
        success(true)
    })
    return UISwipeActionsConfiguration(actions: [delete])
}

您应该使用
cellForRowAt indexPath
设置数据不
willDisplayCell

这是格式化的代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "productCell", for: indexPath) as! ProductTableViewCell 
    cell.product = myFavoritesProductsDataSource[indexPath.row]
    return cell
}
当你删除单元格时,你应该这样做

myFavoritesProductsDataSource.remove(at: indexPath.row)

self.tableView.beginUpdates()    
self.tableView.deleteRows(at: [indexPath], with: .automatic)
self.tableView.endUpdates()

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCellEditingStyle.delete {

        myFavoritesProductsDataSource.remove(at: indexPath.row)

        self.tableView.beginUpdates()    
        self.tableView.deleteRows(at: [indexPath], with: .automatic)
        self.tableView.endUpdates()

    }
}

您应该使用
cellForRowAt indexPath
设置数据不
willDisplayCell

这是格式化的代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "productCell", for: indexPath) as! ProductTableViewCell 
    cell.product = myFavoritesProductsDataSource[indexPath.row]
    return cell
}
当你删除单元格时,你应该这样做

myFavoritesProductsDataSource.remove(at: indexPath.row)

self.tableView.beginUpdates()    
self.tableView.deleteRows(at: [indexPath], with: .automatic)
self.tableView.endUpdates()

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == UITableViewCellEditingStyle.delete {

        myFavoritesProductsDataSource.remove(at: indexPath.row)

        self.tableView.beginUpdates()    
        self.tableView.deleteRows(at: [indexPath], with: .automatic)
        self.tableView.endUpdates()

    }
}


您是否从数据源中删除了它们?能否提供用于从数据源中删除数据的代码?如果您尚未从数据源中删除它们,则会发生这种情况,因为您的单元格即使在删除后也会从内存中加载。@Nevin Jethmalani,“您是否从数据源中删除了它们?”-是的。我做了。除了tablView单元格的属性“isHidden”外,其他所有的东西都很有魅力。所有单元格都有自己的数据。只是显示单元格的机制不好。我必须说我的tableView内容比tableView大。我的意思是,屏幕上只能看到2个单元格。当我只是滚动(删除一些单元格之前)所有单元格都可见且工作正常。您能否在indexPath和
numberOfRowsInSection
中发布
CellForRow的代码?您能否发布如何从数据源中删除它们的代码?@Nevin Jethmalani,我正在“willDisplaycell”中设置数据方法。这是第一个问题,您应该在
cellForRowAt indexPath
中设置数据。然后您可以发布该代码吗?您是否从数据源中删除了它们?您是否可以提供用于从数据源中删除数据的代码?如果您没有从数据源中删除它们,则会发生这种情况,因为您的单元格即使在您删除它们之后,也会从内存中加载。@Nevin Jethmalani,“您从数据源中删除了它们吗?”-是的。我删除了。除了属性“isHidden”之外,所有其他东西都像符咒一样工作所有单元格都有自己的数据。只是显示单元格的机制不太好。我必须说我的tableView内容比tableView大。我的意思是屏幕上只能看到2个单元格。当我只是滚动(删除一些单元格之前)所有单元格都可见且工作正常。您能否在indexPath
numberOfRowsInSection
中发布
CellForRow的代码?您能否发布如何从数据源中删除它们的代码?@Nevin Jethmalani,我正在“willDisplaycell”中设置数据方法。这是第一个问题,您应该在
cellForRowAt indexPath
中设置数据。然后您可以发布该代码吗?但它仍然对我没有帮助。我需要将“cell.ishiden=false”放在对于显示,在您按照上面的方式从数据源中删除项之后,您不需要使用
cell.ishiden=false
Nevin Jethmalani,是的,我有完全相同的代码,这对我没有帮助。可以尝试
let cell=tableView.dequeueReusableCell(标识符为:“productCell”,用于:indexath)作为!ProductTableViewCell
而不是
let cell=tableView.dequeueReusableCell(标识符为:“productCell”)作为!ProductTableViewCell
let cell=tableView.dequeueReusableCell(标识符为:“productCell”,for:indexPath)作为!ProductTableViewCell-没有帮助:(但它仍然对我没有帮助。我需要将“cell.isHidden=false”对于显示,在您按照上面的方式从数据源中删除项之后,您不需要使用
cell.ishiden=false
Nevin Jethmalani,是的,我有完全相同的代码,这对我没有帮助。可以尝试
let cell=tableView.dequeueReusableCell(标识符为:“productCell”,用于:indexath)作为!ProductTableViewCell
而不是
let cell=tableView.dequeueReusableCell(标识符为:“productCell”)作为!ProductTableViewCell
let cell=tableView.dequeueReusableCell(标识符为:“productCell”,for:indexPath)作为!ProductTableViewCell-没有帮助:(