ios - UITableViewCell 没有正确隐藏 UIView

标签 ios swift uitableview

我想隐藏/取消隐藏 UITableViewCell 中的 UIView,但很多时候它会为错误的 UITableViewCell 取消隐藏。有什么建议吗?


cellForRowAtIndexPath 函数

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCellWithIdentifier("locationCell", forIndexPath: indexPath) as? UITableViewCell
        var viewWithImage  =  cell?.viewWithTag(22) as UIView!
        var cellHiddenGemView  =  viewWithImage?.viewWithTag(23) as UIView!                        
        var locationObject : PFObject = locationObjects[indexPath.row] as! PFObject
        var isSecret =  locationObject["isSecret"] as! Bool

        cellHiddenGemView?.hidden = true;

                if isSecret == true
                {
                        cellHiddenGemView?.hidden = false;
                       //this view is unhides for the wrong indexes also
                }        

                return cell;

            }

最佳答案

使用标签而不是子类化 UITableViewCell 并使用 IBOutlets 绝对不是最佳解决方案。

如果您将 UITableViewCell 子类化,您可以覆盖 prepareForReuse() 并将 hidden 属性重置为 true

func prepareForReuse()
{
   self.HiddenGemView?.hidden = true
}

关于ios - UITableViewCell 没有正确隐藏 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31767500/

相关文章:

ios - UICollectionView 捕捉到 0,0 - 滚动超过边界

json - 在 Swift 中调用 api 时无法加载 JSON

ios - 无法在 Storyboard中的 UITableView 上方添加 UIView

iphone - 更改 uitableviewcell 的宽度

ios - 谷歌地图上的标记标题

ios - 将 ViewController View 添加为 subview 的错误框架

swift - 摩尔斯电码转换器无法快速工作

ios - 为什么当 tableview 样式在 iOS 中分组时,部分中的标题 View 无法正确显示, objective-c

ios - Firebase 动态链接无法在安装后继续存在

ios - ios中的Pubnub使用什么密码和 key