【问题标题】:swift uitableview show only different value of an user Arrayswift uitableview 仅显示用户数组的不同值
【发布时间】:2016-02-17 18:13:44
【问题描述】:

我正在开发一个带有解析的消息应用程序。

我想按发件人过滤查询结果,但它为每条消息重复发件人。

我认为可以在数组上找到相同的值吗? (在这个例子中的clientsArray)

有什么想法吗?

在我使用的代码下方。

var clientName = ""
var clientsArray:[String] = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    //Message List Query
    let messages = PFQuery(className:"Message")
    messages.whereKey("user", equalTo:PFUser.currentUser()!["retailer"]!)

    messages.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {

            print("Successfully retrieved \(objects!.count) scores.")

            if let objects = objects as? [PFObject] {

                self.clientName.removeAll()

                for object in objects {

                    if let clientName = object["clientName"] as? String {
                        self.clientsArray.append(clientName)
                    }

                    self.tableView.reloadData()

                    print(self.clientsArray)

                }
            }
        } else {
            print(error)
        }
    }        
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return clientsArray.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("clientsCell", forIndexPath: indexPath)
    let clientName = clientsArray[indexPath.row]

    if let clientNameLabel = cell.viewWithTag(301) as? UILabel {
        clientNameLabel.text = clientName
    }

    return cell
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "showChat" {

        if let destination = segue.destinationViewController as? newChatViewController {

            destination.clientName = clientsArray[(tableView.indexPathForSelectedRow?.row)!]

            //self.hidesBottomBarWhenPushed = true
        }
    }
}

【问题讨论】:

标签: swift uitableview parse-platform


【解决方案1】:

如果不看数据结构是什么样子,真的很难回答。但我可以告诉你代码的问题。

 if let clientName = object["clientName"] as? String {
     self.clientsArray.append(clientName)
 }

您的 if-let 语句只是获取 ["clientName"] 的对象,它只是您的客户名称。然后您将您的客户名称附加到您的 clientsArray 对象中。

这就是为什么你只是看到卢克。

我可以帮助解决问题,但我需要知道您的数据结构是什么样的。你能打印出整个对象吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 2012-10-30
    • 1970-01-01
    • 2015-05-09
    相关资源
    最近更新 更多