无法在tableView中根据字典中的项目创建多个节

时间:2016-02-26 09:34:30

标签: ios swift uitableview

你好我有这样的字典

  {
        0 =     {
            ProductRequest =         {
                description = "";
                "destination_country" = "";
            };
            User =         {
                "first_name" = irfan;
                "last_name" = sheikhg;
            };
        };
        1 =     {
            ProductRequest =         {
                description = dummy;
                "destination_country" = dummy;
                "user_id" = 9;
            };
            User =         {
                "first_name" = john;
                "last_name" = Doe;
            };
        };
        code = 200;
    }

如果我返回这样的部分,我的程序会崩溃

return dict.count - 1

如果我这样做

return dict.count

程序没有崩溃,但它会返回一个空的部分。通过查看我的数组,您将知道我在数组中发送成功代码的第3个元素,这就是我正在做的dict.count - 1

完整的类代码

  class RequestTableViewController: UITableViewController {
  var dict = NSDictionary()

  override func viewDidLoad() {
    super.viewDidLoad()
    getRequestsFromServer()
  }

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

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

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! RequestTableViewCell
    if(indexPath.section == 0) {
      cell.requestTitleTextView.text = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["ProductRequest"] as?NSDictionary)!["request_title"] as?NSString)! as String

      let firstName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["User"] as?NSDictionary)!["first_name"] as?NSString)! as String
      let  lastName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["User"] as?NSDictionary)!["last_name"] as?NSString)! as String

      cell.userNameLabel.text = firstName.uppercaseString + " " +  lastName.uppercaseString
    }
    return cell
  }

  func getRequestsFromServer() {
    let url:String = Variables.GETALLREQUESTS_URI
    let params = ["data":"get"]
    ServerRequest.postToServer(url, params: params) { result, error in

      if let result = result {
        let code =  result["code"] as? Int
        print(result)
        if(code == 200) {
          self.dict = result
          dispatch_async(dispatch_get_main_queue(), {
            // DO SOMETHING ON THE MAINTHREAD
            self.tableView.reloadData()
          })

        } else if(code==500 ) {
          dispatch_async(dispatch_get_main_queue(), {
            AlertView().showAlert(Message.INTERNETISNOTCONNECTED)
            /*let alert = UIAlertView()
            alert.message = Message.INTERNETISNOTCONNECTED
            alert.addButtonWithTitle("Ok")
            alert.show()*/
          })
        }
      }
    }
  }
}

更新

当它崩溃时。什么都没有在控制台打印出来。这是图像 enter image description here

1 个答案:

答案 0 :(得分:0)

我认为@Wain正在做点什么:)

如果您的dict最初是空的,那么您将返回-1作为部分的数量 - 这可能不会起作用。您需要在numberOfSections方法中检查:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    guard dict.count > 0 else { return 0 }

    return dict.count - 1
}

虽然处理这个问题的一个更好的方法可能是更聪明地计算你如何计算部分的数量;如果服务器再次返回除“代码”之外的任何其他密钥,那么从计数中减去1将会遇到麻烦。 。 。