xcode - 如何在 Swift 中使用 Parse 植入分区 TableView

标签 xcode uitableview swift parsing

我正在努力弄清楚如何使用解析来实现分段 TableView 。如果我不加载我想要的名称,我可以正确地划分表格 View 。但是,当我使用 parse 时,名称不会及时加载,因此会因为它是异步的而被遗漏。

当我在 queryFriends() 中重新加载表时,它没有显示。我的理论是表格没有再次分区。

如何重新制作表格部分?

或者有没有人有什么想法?

谢谢你的帮助

import UIKit
import Parse
import Foundation

class MyFriendsTableView: UITableViewController, UITableViewDataSource, UITableViewDelegate{


var names:[String] = []

/*var names: [String] = [
    "AAAA",
    "Clementine",
    "Bessie",
    "Yolande",
    "Tynisha",
    "Ellyn",
    "Trudy",
    "Fredrick",
    "Letisha",
    "Ariel",
    "Bong",
    "Jacinto",
    "Dorinda",
    "Aiko",
    "Loma",
    "Augustina",
    "Margarita",
    "Jesenia",
    "Kellee",
    "Annis",
    "Charlena",
    "!#@",
    "@@@"
]*/




override func viewDidLoad() {
    super.viewDidLoad()



    self.refreshControl = UIRefreshControl()
    self.refreshControl?.attributedTitle = NSAttributedString(string: "Pull to refresh")
    self.refreshControl?.addTarget(self, action: "queryFriends", forControlEvents: UIControlEvents.ValueChanged)
    self.tableView.addSubview(refreshControl!)





}


override func viewDidAppear(animated: Bool) {

    UIApplication.sharedApplication().statusBarHidden = false
    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent

    queryFriends()




}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func queryFriends() {
    let currentUser = PFUser.currentUser()?.username
    let predicate = NSPredicate(format: "status = 'Friends' AND fromUser = %@ OR status = 'Friends' AND toUser = %@", currentUser!, currentUser!)
    var query = PFQuery(className: "FriendRequest", predicate: predicate)
    var friends:[String] = []
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {
            if let objects = objects as? [PFObject] {
                for object in objects {
                    if object["toUser"] as? String != currentUser {
                        friends.append(object["toUser"] as! String)

                    } else if object["fromUser"] as? String != currentUser {
                        friends.append(object["fromUser"] as! String)

                    }
                }

            }

        } else {
            // Log details of the failure
            println("Error: \(error!) \(error!.userInfo!)")
        }
        self.names = friends
        self.tableView.reloadData()
        self.refreshControl?.endRefreshing()
    }
}



/* type to represent table items
`section` stores a `UITableView` section */
class User: NSObject {
    let name: String
    var section: Int?

    init(name: String) {
        self.name = name
    }
}

// custom type to represent table sections
class Section {
    var users: [User] = []

    func addUser(user: User) {
        self.users.append(user)
    }
}


// `UIKit` convenience class for sectioning a table
let collation = UILocalizedIndexedCollation.currentCollation()
    as! UILocalizedIndexedCollation

// table sections
var sections: [Section] {
    // return if already initialized
    if self._sections != nil {
        return self._sections!
    }

    // create users from the name list
    var users: [User] = names.map { name in
        var user = User(name: name)
        user.section = self.collation.sectionForObject(user, collationStringSelector: "name")
        return user
    }

    // create empty sections
    var sections = [Section]()
    for i in 0..<self.collation.sectionIndexTitles.count {
        sections.append(Section())
    }

    // put each user in a section
    for user in users {
        sections[user.section!].addUser(user)
    }

    // sort each section
    for section in sections {
        section.users = self.collation.sortedArrayFromArray(section.users, collationStringSelector: "name") as! [User]
    }

    self._sections = sections

    return self._sections!

}
var _sections: [Section]?

// table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return self.sections.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.sections[section].users.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let user = self.sections[indexPath.section].users[indexPath.row]
        let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell", forIndexPath: indexPath) as! UITableViewCell
        cell.textLabel!.text = user.name
        return cell
}

/* section headers
appear above each `UITableView` section */
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        // do not display empty `Section`s
        if !self.sections[section].users.isEmpty {
            return self.collation.sectionTitles[section] as? String
        }
        return ""
}

/* section index titles
displayed to the right of the `UITableView` */
override func sectionIndexTitlesForTableView(tableView: UITableView) -> [AnyObject] {
        return self.collation.sectionIndexTitles
}

override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
        return self.collation.sectionForSectionIndexTitleAtIndex(index)
}

最佳答案

您可以尝试清除queryFriends中的_sections:

self._sections = nil
self.names = friends
self.tableView.reloadData()
self.refreshControl?.endRefreshing()

关于xcode - 如何在 Swift 中使用 Parse 植入分区 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30701390/

相关文章:

ios - 代码签名错误。添加新的mac进行开发

ios - (iOS,Xcode)有什么方法可以覆盖 "tap status bar to scroll up"行为吗?

ios - 如何将 AdMob 集成到 iOS 应用中?其他教程似乎不适用于最新更新

php - 将 id 从 iOS 应用程序发送到 php 到 mysql

objective-c - 无法将表格重新排序才能正常工作

UnsafeMutableRawPointer 的 Swift 3 转换问题 - AudioQueue

ios - 弹出菜单继续

ios - iOS 模拟器上方和下方的黑条

java - 为不同的设备生成服务器端图像?

swift - 从 Parse.com 获取数据到 swift