如何在'empty'(不可点击)时制作原型单元格'disappear'

How to make a prototype cell 'disappear' when it is 'empty' (not clickable)

我正在制作一个应用程序,用户可以在其 x km 半径内看到某些 items/users(很像 Tinder,您可以在其中设置您想要在您所在区域看到的 girls/guys 的半径) .所以在我的 cellForRowAtIndexPath 函数中,我正在确定是否可以显示一个单元格。

如果他在半径内,则显示该事件。如果位置太远,则不应使用小区。

我当前的代码只是隐藏了单元格,但它仍然是可点击的。我希望它一开始就不要使用单元格,但我找不到该怎么做。有什么想法吗?

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> AllGeneralMeetsTableViewCell {
        //get location from backend
        let locLati = object?["coordLat"] as? Double
        let locLongi = object?["coordLong"] as? Double

        let currentLocation:CLLocation = CLLocation(latitude: localLati , longitude: localLongi)
        let meetLocation:CLLocation = CLLocation(latitude: locLati! , longitude: locLongi!)
        let meters:CLLocationDistance = currentLocation.distanceFromLocation(meetLocation)
        // make distance in km
        let distInKm = meters/1000
        //get distance that user has set in his settings
            let cell = tableView.dequeueReusableCellWithIdentifier("GeneralMeetsIdentifier") as! AllGeneralMeetsTableViewCell!
        if (distInKm <= settingsKm) {
        // Extract values from the PFObject to display in the table cel
            if let title = object?["title"] as? String {
                cell?.titleCell?.text = title
            }
            if let message = object?["message"] as? String {
                cell?.messageCell?.text = message
            }
            if let image = object?["image"] as? PFFile {
                image.getDataInBackgroundWithBlock({ (imageData: NSData?,error: NSError?) -> Void in
                    if (error == nil) {
                        let image1 = UIImage(data:imageData!)
                        cell.imageCell.image = image1
                    }
                })

            }

            return cell
        }
        else {
        return cell
        }
    }
}

以下查询正在返回数据

override func queryForTable() -> PFQuery {
    let query = PFQuery(className: self.parseClassName!)
    let FBID = myUser.objectForKey("facebookID")!
    query.whereKey("facebookID", equalTo: FBID)
    query.whereKey("private", equalTo: "false")
    return query
}

你想让单元格在没有数据时消失。如果我是对的,那么您可以使用这个名为“DZNEmptyDataSet”的库来显示一些图像,告诉用户没有要加载的数据。使用 Cocoapods 来安装它,或者只是拖动文件并创建一个桥接头。用法也非常简单 - 只需按照 GitHub.

中的文档进行操作即可

在你的 cellForRowAtIndexPath-

if cell is to be displayed {
    tableView.rowHeight = 120
    //Replace 120 with desired rowHeight
} else {
    tableView.rowHeight = 0
}

希望这对您有所帮助:-)

Parse 提供了一种类型 PFGeopoint 甚至在 Query 中也支持它,使用 Parse 框架你也可以像从数据库下载东西一样获取位置,这样你就可以只下载那些帖子可供他范围内的用户使用...

PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint: PFGeoPoint?, error: NSError?) -> Void in if error == nil { //asynchronous, U have access to GPS location // create Parse Query let query: PFQuery = PFQuery(className: "Post") // 5km range query.whereKey("gps", nearGeoPoint: geoPoint, withinKilometers: 5.0) //rest of query.... } else { // dont have access to GPS or whatever print("Eror location: \(error)") } }

正确的解决方案是从您的数据源中删除数据,这样它就不会首先显示。但有时这是很多工作。

如果您只想隐藏一个单元格,您可以使用 tableView(_:heightForRowAtIndexPath:) 委托方法将其设置为零高度:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.row == 1 {
        return 0.0
    }

    return tableView.rowHeight
}