隐藏特定单元格

时间:2015-05-26 22:04:17

标签: ios uitableview swift

我遇到了UITableViewCell的问题。

当我生成tableCells时,我检查一个值是否等于某个值,如果是,我隐藏了我在原型单元格中制作的自定义按钮。问题是,带有隐藏标识符的按钮隐藏在每个Cell中,而不是当前只有...

我环顾四周但找不到任何事情......你能帮忙吗?这是我的代码,请看if currentPost.valueForKey... == "noLink"行。谢谢 !

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        var cell = tableView.dequeueReusableCellWithIdentifier("postCell", forIndexPath: indexPath) as! PostTableViewCell
        var currentCell = indexPath
        var currentPost = post[indexPath.row]
        var currentUser = followedInfo[currentPost.valueForKey("userID") as! String]

        cell.username.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left


        cell.userProfil.layer.cornerRadius = 0.5 * cell.userProfil.bounds.size.width
        cell.username.setTitle(currentUser?.valueForKey("username") as? String, forState: UIControlState.Normal)
        cell.postArtist.text = currentPost.valueForKey("artistName") as? String
        cell.postDescription.text = currentPost.valueForKey("postDescription") as? String
        cell.postTitle.text = currentPost.valueForKey("songName") as? String
        cell.postTime.text = "7J"
        cell.postDescription.sizeToFit()

        if currentPost.valueForKey("itunesLink") as? String == "noLink" {

            cell.postItunes.hidden = true;

        }else{
            cell.postItunes.addTarget(self, action: "getToStore:", forControlEvents: .TouchUpInside)
        }

        if currentPost.valueForKey("previewLink") as? String == "noPreview" {
            cell.postPlay.alpha = 0;
        }else{
            cell.postPlay.addTarget(self, action: "getPreview:", forControlEvents: .TouchUpInside)
        }

        if currentPost.valueForKey("location") as? String == "noLocalisation" {
            cell.postLocation.text = "Inconnu"
        }else{
            cell.postLocation.text = currentPost.valueForKey("location") as? String
        }


        if currentUser?.valueForKey("profilePicture")! != nil{
            var pictureFile: AnyObject? = currentUser?.valueForKey("profilePicture")!
            pictureFile!.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
                var theImage = UIImage(data: imageData!)
                cell.userProfil.image = theImage
            })
        }

        if currentPost.valueForKey("coverLink") as? String == "customImage" {
            var profilPictureFile: AnyObject? = currentPost.valueForKey("postImage")

            profilPictureFile!.getDataInBackgroundWithBlock { (imageData , imageError ) -> Void in

                if imageError == nil{
                    let image = UIImage(data: imageData!)
                    cell.postPicture.image = image
                }
            }
        }else{
            if currentPost.valueForKey("coverLink") as? String == "noCover"{

                var cover = UIImage(named: "noCover")
                cell.postPicture.image = cover

            }else{
                var finalURL = NSURL(string: currentPost.valueForKey("coverLink") as! String)
                let request: NSURLRequest = NSURLRequest(URL: finalURL!)
                NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
                    if error == nil {
                        var image = UIImage(data: data)


                        cell.postPicture.image = image

                    }
                    else {
                        println("Error: \(error.localizedDescription)")
                    }
                })
            }
        }









        return cell;
    }

1 个答案:

答案 0 :(得分:3)

如果要显示底部,则不会将底部设置为可见,因为您正在重复使用单元格,最后一次设置是单元格的一部分,因此只要将其设置为隐藏为true,它将一直隐藏,直到如果将hidden设置为false,则使用可重用单元格的最佳方法是始终将所有选项设置为您希望该单元格的选项,以避免将该设置设置为旧单元格。

    if currentPost.valueForKey("itunesLink") as? String == "noLink" {
        cell.postItunes.hidden = true;
    }else{
        cell.postItunes.hidden = false;
        cell.postItunes.addTarget(self, action: "getToStore:", forControlEvents: .TouchUpInside)
    }