如果空UITableView单元格中没有数据,如何自动删除它们?

时间:2015-10-21 16:30:42

标签: ios swift uitableview

编辑1:对象声明(转到帖子的底部)

我正在从JSON(通过我的API下载)加载数据,并且出于某种原因,在过去的几天里,我已经将单元格显示为空,在完美构建的单元格与实际之间数据。如何让那些空单元格自动消失? (所以细胞之间没有间隙)?

这是我的代码,我从JSON和我的数据源代码创建对象:

对象代码:

  func populateCurrentIssue() {
    if populatingCurrentIssue {
        return
    }

    populatingCurrentIssue = true

    self.cellLoadingIndicator.backgroundColor = UIColor.yellowColor()
    self.cellLoadingIndicator.startAnimating()

    Alamofire.request(GWNetworking.Router.Issue).responseJSON() { response in
        if let JSON = response.result.value {

            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {

                var nodeIDArray : [Int]

                if (JSON .isKindOfClass(NSDictionary)) {

                    for node in JSON as! Dictionary<String, AnyObject> {

                        let nodeIDValue = node.0
                        var lastItem : Int = 0

                        if let issueElement : IssueElement = IssueElement(title: "Could not retrieve title", nodeID: 0, timeStamp: 0, imageURL: "init", author: "Author not found", issueNumber: "Issue # error", volumeNumber: "Volume # error", articleContent: "Could not retrieve article content", coverImageInteger: "init", coverImage: UIImage()) {

                            issueElement.title = node.1["title"] as! String
                            issueElement.nodeID = Int(nodeIDValue)!

                            let timeStampString = node.1["revision_timestamp"] as! String
                            issueElement.timeStamp = Int(timeStampString)!

                            issueElement.imageURL = String(node.1["image_url"])

                            if let author = node.1["author"] as? String {
                                issueElement.author = author
                            }

                            issueElement.volumeNumber = String(node.1["volume_int"])

                            if let articleContent = node.1["html_content"] as? String {
                                issueElement.articleContent = articleContent
                            }

                            issueElement.coverImageInteger = String(node.1["cover_image"]) // addition specific to the Current Issue View Controller

                            lastItem = self.currentIssueObjects.count

                            print(issueElement.nodeID)

                            self.currentIssueObjects.addObject(issueElement)


                            let timestampSortDescriptor = NSSortDescriptor(key: "timeStamp", ascending: false)
                            self.currentIssueObjects.sortUsingDescriptors([timestampSortDescriptor])



                            let coverImageSortDescriptor = NSSortDescriptor(key: "coverImageInteger", ascending: false)
                            self.currentIssueObjects.sortUsingDescriptors([coverImageSortDescriptor])

                            print(self.currentIssueObjects)

                            let indexPaths = (lastItem..<self.currentIssueObjects.count).map {
                                NSIndexPath(forItem: $0, inSection: 0) }
                            }
                        }

                    }

                dispatch_async(dispatch_get_main_queue()) {
                    self.currentIssueTableView.reloadData()

                    self.cellLoadingIndicator.stopAnimating()
                    self.cellLoadingIndicator.hidesWhenStopped = true

                }

            }

        }

        self.populatingCurrentIssue = false
}

}

数据源代码:

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

    let row = indexPath.row

    guard let cell = tableView.dequeueReusableCellWithIdentifier(CurrentIssueArticlesTableCellIdentifier, forIndexPath: indexPath) as? CurrentIssueArticlesTableViewCell else {

        print ("error: currentIssueTableView cell is not of class CurrentIssueArticlesTableViewCell, we will use EditorialsTableViewCell instead")
        return tableView.dequeueReusableCellWithIdentifier(CurrentIssueArticlesTableCellIdentifier, forIndexPath: indexPath) as! EditorialsTableViewCell
    }



    let currentIssueObject = currentIssueObjects.objectAtIndex(indexPath.row) as! IssueElement

        let title = currentIssueObject.title ?? ""

        let timeStampDateObject = NSDate(timeIntervalSince1970: NSTimeInterval(currentIssueObject.timeStamp))
        let timeStampDateString = dateFormatter.stringFromDate(timeStampDateObject) ?? "Date unknown"



    if let author = currentIssueObject.author {

        cell.currentIssueArticlesAuthorLabel!.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
        cell.currentIssueArticlesAuthorLabel!.text = author

    } else {
    }


        let issueNumber = currentIssueObject.issueNumber ?? ""
        let volumeNumber = currentIssueObject.volumeNumber ?? ""

        let articleContent = currentIssueObject.articleContent ?? ""

        let nodeID = currentIssueObject.nodeID ?? 0

        cell.currentIssueArticlesHeadlineLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
        cell.currentIssueArticlesHeadlineLabel.text = title



        cell.currentIssueArticlesPublishDateLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
        cell.currentIssueArticlesPublishDateLabel.text = timeStampDateString

        if row == 0 {

            cell.userInteractionEnabled = false

            let imageURL = (currentIssueObjects.objectAtIndex(row) as! IssueElement).imageURL

            cell.currentIssueArticlesHeadlineLabel.textColor = UIColor.clearColor()
            cell.currentIssueArticlesAuthorLabel.textColor = UIColor.clearColor()
            cell.currentIssueArticlesPublishDateLabel.textColor = UIColor.clearColor()

            cell.request?.cancel()

            if let image = self.imageCache.objectForKey(imageURL!) as? UIImage {
                cell.currentIssueArticlesBackgroundImageView.image = image
            } else {
                cell.currentIssueArticlesBackgroundImageView.image = UIImage(named: "reveal Image")
                cell.request = Alamofire.request(.GET, imageURL!).responseImage() { response in
                    if response.result.error == nil && response.result.value != nil {

                        self.imageCache.setObject(response.result.value!, forKey: response.request!.URLString)

                        cell.currentIssueArticlesBackgroundImageView.image = response.result.value
                    } else {

                    }

                }
            }


        }

        else {



        }

    if (cell.currentIssueArticlesAuthorLabel == nil) {
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }

    return cell
}

编辑1:对象声明

class IssueElement: NSObject {

var title: String           // title
var nodeID: Int             // nid
var timeStamp: Int          // revision_timestamp
var imageURL: String?      // image_url
var author: String?          // author

var issueNumber: String     // issue_int
var volumeNumber: String    // volume_int

var articleContent: String // html_content

var coverImageInteger: String // Variable that indicates whether this is the cover page or not (1 for cover, 0 for everything else)

var coverImage: UIImage

init(title: String, nodeID: Int, timeStamp: Int, imageURL: String?, author: String, issueNumber: String, volumeNumber: String, articleContent: String, coverImageInteger: String, coverImage: UIImage) {
    self.title = title
    self.nodeID = nodeID
    self.timeStamp = timeStamp
    self.imageURL = imageURL
    self.author = author
    self.issueNumber = issueNumber
    self.volumeNumber = volumeNumber
    self.articleContent = articleContent
    self.coverImageInteger = coverImageInteger
    self.coverImage = coverImage
}

override func isEqual(object: AnyObject!) -> Bool {
    return (object as! IssueElement).nodeID == self.nodeID
}

override var hash: Int {
    return (self as IssueElement).nodeID
}

}

2 个答案:

答案 0 :(得分:1)

实际上你不想这样做,因为途中还会有其他问题。例如,编辑单元格中数据的最简单方法是将单元格的indexPath.row链接到dataSource(例如,第二个单元格链接到dataSource中的第二个元素,第三个链接到第三个元素,依此类推)。如果您过滤单元格而不是数据,那么您将打破这种逻辑。

因此,最好的方法是过滤数据,只需在将空元素添加到UITableView之前删除它们。

答案 1 :(得分:0)

我同意Lithium的初步分析。在将数据添加到数组之前先过滤数据。如果数据是字符串,那么可以在添加到数组之前尝试字符计数验证,如下所示:

if issueElement.characters.count > 0 { //Add to Array... }