在swift中使用多个原型单元仅在取消激活时才有效

时间:2015-08-17 09:55:20

标签: swift uitableview

我的桌面视图有2个原型单元格。一个有图像,另一个没有。

当我在模拟器上运行应用程序时,我的tableview会相应地显示正确的行,如果它们有图像的话。但是这些行没有内容。无论是文字还是图片。

但是,如果我评论let standardCell并仅使用let imageCell或反之亦然,我会填充行,但会在原型中进行评论。

这是我的输出,在这里您可以看到我从数据库中获取所有内容,而且我也触发了正确的单元格。

HAS IMAGE?->Optional(false) AT INDEX PATH -> 0
STANDARD CELL AT->(0, Optional("no image"))
HAS IMAGE?->Optional(true) AT INDEX PATH -> 1
IMAGE CELL AT->(1, Optional("image"))
HAS IMAGE?->Optional(false) AT INDEX PATH -> 0
STANDARD CELL AT->(0, Optional("no image"))
HAS IMAGE?->Optional(true) AT INDEX PATH -> 1
IMAGE CELL AT->(1, Optional("image"))
HAS IMAGE?->Optional(true) AT INDEX PATH -> 2
IMAGE CELL AT->(2, Optional("image"))
HAS IMAGE?->Optional(false) AT INDEX PATH -> 3
STANDARD CELL AT->(3, Optional("no image"))
HAS IMAGE?->Optional(true) AT INDEX PATH -> 4
IMAGE CELL AT->(4, Optional("image"))

代码

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

        //Cell identifiers
        let standardCellIdentifier = "StandardCell"
        let imageCellIdentifier = "ImageCell"

        //variables
        var imageForCell = UIImage()
        var testString = String()
//        var hasImage = true

        //cells
        let standardCell = tableView.dequeueReusableCellWithIdentifier(standardCellIdentifier, forIndexPath: indexPath) as! HobbieFeedStandardTVCell
        let imageCell = tableView.dequeueReusableCellWithIdentifier(imageCellIdentifier, forIndexPath: indexPath) as! HobbieFeedImageTVCell

        //get object, text and votes
        let object = self.timelineData.objectAtIndex(indexPath.row) as! PFObject
        var myText = object.objectForKey("postText") as? String
        println("MY TEXT -> \(myText)")
        var hasImage = object.objectForKey("hasImage") as? Bool
        let imageFromParse = object.objectForKey("postImage") as? PFFile
//        var myVotes = object.objectForKey("postVotes") as? String

        //find if there is an image
         println("HASIMAGE IS->\(indexPath.row, hasImage)")

        //get image
        if hasImage == true
        {
            println("RETURNED IMAGE->\(indexPath.row)")
            //set image for cell
            imageFromParse!.getDataInBackgroundWithBlock({ (imageData:NSData?, error:NSError?) -> Void in
                if error == nil {

                    if let myImageData = imageData {
                        let image = UIImage(data:myImageData)
                        imageCell.cellImage!.image = image
                    }
                }
                }, progressBlock: { (percent: Int32) -> Void in
            })

//            imageCell.cellImage.image = UIImage(named: "logoPDF")
            imageCell.cellText.text = "has image"
            return imageCell
        }

            println("RETURNED STANDARD->\(indexPath.row)")
            standardCell.cellText.text = "no image"
            return standardCell

    }

1 个答案:

答案 0 :(得分:1)

每次调用最后一个return语句。将其添加到ifelse

如果你想要一次性去除细胞。

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

        //Cell identifiers
        let standardCellIdentifier = "StandardCell"
        let imageCellIdentifier = "ImageCell"

        //variables
        var imageForCell = UIImage()
        var testString = String()

        let object = self.timelineData.objectAtIndex(indexPath.row) as! PFObject
        var myText = object.objectForKey("postText") as? String
        println("MY TEXT -> \(myText)")
        var hasImage = object.objectForKey("hasImage") as? Bool
        let imageFromParse = object.objectForKey("postImage") as? PFFile
        //find if there is an image
         println("HASIMAGE IS->\(indexPath.row, hasImage)")

        //get image
        if hasImage == true
        {
            let imageCell = tableView.dequeueReusableCellWithIdentifier(imageCellIdentifier, forIndexPath: indexPath) as! HobbieFeedImageTVCell
            println("RETURNED IMAGE->\(indexPath.row)")
            //set image for cell
            imageFromParse!.getDataInBackgroundWithBlock({ (imageData:NSData?, error:NSError?) -> Void in
                if error == nil {

                    if let myImageData = imageData {
                        let image = UIImage(data:myImageData)
                        imageCell.cellImage!.image = image
                    }
                }
                }, progressBlock: { (percent: Int32) -> Void in
            })
            imageCell.cellText.text = "has image"
            return imageCell
        }
        else{
            let standardCell = tableView.dequeueReusableCellWithIdentifier(standardCellIdentifier, forIndexPath: indexPath) as! HobbieFeedStandardTVCell
            println("RETURNED STANDARD->\(indexPath.row)")
            standardCell.cellText.text = "no image"
            return standardCell
        }
    }