ios - 无法使用 String 类型的索引下标 NSMutableDictionary 类型的值

标签 ios swift

我也遇到了上面提到的问题。有人可以帮我吗? 我已将我的 NSMutableDictionary 定义为结果。通过打印它,我得到了如下结果:

下面是“resultsNSMutableDictionary 中的值。

[{Name = 4480101010;Feedback = goo;FeedbackID = 186;FeedbackOn = "18-Mar-2016 11:26";IsFrom = 1;UserName = "Bernie Killian";RequestID = 1531;}

{Name = 4480101010;Feedback = supr;FeedbackID = 172;FeedbackOn = "09-Mar-2016 08:04";IsFrom = 1;UserName = rajesh;RequestID = 1445;},{Name = 4480101010;Feedback = supr;FeedbackID = 170;FeedbackOn = "09-Mar-2016 08:00";IsFrom = 1;UserName = rajesh;RequestID = 1444;},{Name = 4480101010;Feedback = "all works fine";FeedbackID = 158;FeedbackOn = "08-Mar-2016 17:21";IsFrom = 1;UserName = "Mahendra Suthar";RequestID = 1429;}]

我需要在表格 View 中填充这些值,这样名称应该在 nameLabel 单元格值中,反馈在 feedbackLabel 单元格值中。怎么做?我已经尝试了所有可能的事情。但无法得到解决方案。

这是我的完整代码。

导入基金会 反馈 View Controller 类:UIViewController、UITableViewDelegate、UITableViewDataSource{

@IBOutlet weak var tableView: UITableView!
var feedbackResults = [String]()
var prefectCount : Int = 0
var abhyasiCount : Int = 0
var prefectResults = [NSMutableDictionary]()
var abhyasiResults = [NSMutableDictionary]()
var selectedIndex : Int = 0
var totalCount : Int = 1

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
    reloadTable()
    let appid : String = NSUserDefaults.standardUserDefaults().objectForKey("appID") as! String
    Server.sendGet(URL.getUserFeedbackInformation(appid), completionHandler: self.feedbackCompletionHandler)
}

@IBAction func onFeedbackChanged(sender: AnyObject) {
    if sender.selectedSegmentIndex == 0 {
        self.selectedIndex = 0
    }else{
        self.selectedIndex = 1
    }
    print(selectedIndex)
}

func feedbackCompletionHandler(data: NSData?, response: NSURLResponse?, error: NSError?){
    dispatch_async(dispatch_get_main_queue(), {
        // code here
        let responseStr = Server.dataToNSArray(data)
        for eachFeedback in responseStr!{
            if eachFeedback["IsFromPerceptor"] as! NSNumber == 1   {
                self.abhyasiCount++
                self.abhyasiResults.append(eachFeedback as! NSMutableDictionary)

            }
            else
            {
                self.prefectCount++
                self.prefectResults.append(eachFeedback as! NSMutableDictionary)

            }
        }

        self.reloadTable()

    })

}

// START Table View
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1

}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print(self.selectedIndex)
    if self.selectedIndex == 0 {
        self.totalCount = self.abhyasiCount
    }else{
        self.totalCount =  self.prefectCount
    }
    return self.totalCount

}


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

    let cell = tableView.dequeueReusableCellWithIdentifier("FeedbackIdentifierCell", forIndexPath: indexPath) as! FeedbackIdentifierCell
    cell.numberLabel.text = abhyashiResults[“FeedbackID”] as? String
    cell.nameLabel.text = abhyashiResults[“Name”] as? String
    cell.feedbackLabel.text = abhyashiResults[“Feedback”] as? String
    cell.dateLabel.text = abhyashiResults[“FeedbackOn”] as? String

    return cell

}

func reloadTable() {
    tableView.reloadData()
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

最佳答案

您提供的结果值是一个字典数组,而不是字典本身。根据您的评论,您似乎需要从数组中提取特定值。您可能会根据传递给您的索引路径执行此操作:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("FeedbackIdentifierCell", forIndexPath: indexPath) as! FeedbackIdentifierCell

    let valueForCell = results[indexPath.row]
    cell.numberLabel.text = valueForCell["Feedback"]
    …

关于ios - 无法使用 String 类型的索引下标 NSMutableDictionary 类型的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36299799/

相关文章:

ios - Swift 字典中的属性观察器

ios - 制作新格式(例如 ".rcb")

ios - 检测不在 UITextField 上的触摸(隐藏键盘)

ios - UIActivityController 未在屏幕上完整显示

macos - 如何在 Swift 中获取按下的键

swift - 纹理中的 SKPhysicsBody 存在问题

ios - 了解何时通过手势关闭分屏 View Controller 的主控

ios - 更改 UIBarButton 内按钮内图标的颜色(Swift)

swift - 在 Swift 中定义匿名 CollectionType 的最简单方法

swift - 快速获取图像的 Alpha 百分比