如何获取所选动作表选择器Swift的indexpath

时间:2019-06-15 05:01:21

标签: json swift

我陷入混乱。我需要有人来帮助我。 我一直在尝试使用动作表选择器,并且一切都很好,但是我需要获取选定的项目。我该怎么办?

 ActionSheetMultipleStringPicker.show(withTitle: "Select Country", rows: [
        countriesArray,], initialSelection: [0],
    doneBlock: {
            picker, indexes, values in


            print("values = \(values)")
            print("indexes = \(indexes)")
            print("picker = \(picker)")

                DispatchQueue.main.async {
                    // Update UI
                    self.performSegue(withIdentifier: "pdfsegue", sender: nil)
                }
            return
    }, cancel:
        {
            ActionMultipleStringCancelBlock in return

    }, origin: sender)

这些是我的数组:

var emiratesArray = [String]()
var emiratesIdArray = [Int]()

我正在从JSON获取值到我的数组中:

let url = NSURL(string: EMIRATES_URL)
    URLSession.shared.dataTask(with: (url as?URL)!, completionHandler: {(data,response,error) ->
        Void in

        if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary
        {

            print(jsonObj.value(forKey: "data")!)


            if let messageArray = jsonObj.value(forKey: "data") as? NSArray
            {
                for message in messageArray
                {
                    if let messageDict = message as? NSDictionary
                    {

                        if let data = data {


                            let successmessage = jsonObj.value(forKey: "success") as? Int

                            if(successmessage == 1)
                            {

                                if let emirate_name = messageDict.value(forKey: "emirate_name")
                                {
                                    self.emiratesArray.append(emirate_name as! String)
                                    print(emirate_name)

                                }

                                if let company_id = messageDict.value(forKey: "id")
                                {
                                    self.emiratesIdArray.append(company_id as! Int)
                                    print(company_id)
                                }


                            } else
                            {

                            }

                        }


                    }
                }
            }

        }


    }).resume()

有人帮助我如何获取选定的项目?如果我使用的是tableview,那么我应该从[indexpath.row]中获取ID,但是在UIActionsheet选择器中,我不知道如何获取所选值的ID。请有人帮我

1 个答案:

答案 0 :(得分:0)

在给定的代码中我没有找到任何自定义类。您可以创建如下所示的类:

class Emirate: NSObject {
    var id: Int
    var name: String

    init(id: Int, name: String) {
        self.id = id
        self.name = name
    }

    override var description: String {
        return self.name
    }
}

我已经覆盖了description属性,因为如果ActionSheetMultipleStringPicker不是String,则会显示数组中给定对象的描述。

声明您的Emirate数组

var emiratesArray = [Emirate]()

您的API请求和解析应如下:

let url = NSURL(string: "EMIRATES_URL")
URLSession.shared.dataTask(with: (url as?URL)!, completionHandler: {(data,response,error) ->
    Void in

    if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary
    {

        print(jsonObj.value(forKey: "data")!)


        if let messageArray = jsonObj.value(forKey: "data") as? NSArray
        {
            for message in messageArray
            {
                if let messageDict = message as? NSDictionary
                {

                    if let data = data {


                        let successmessage = jsonObj.value(forKey: "success") as? Int

                        if(successmessage == 1)
                        {

                            if let emirate_name = messageDict.value(forKey: "emirate_name") as? String, let company_id = messageDict.value(forKey: "id") as? Int
                            {
                                self.emiratesArray.append(Emirate(id: company_id, name: emirate_name))
                                print(emirate_name)
                            }


                        } else
                        {

                        }

                    }


                }
            }
        }

    }


}).resume()

现在输入您的操作表self.emiratesArray

ActionSheetMultipleStringPicker.show(withTitle: "Select Country", rows: [
    self.emiratesArray], initialSelection: [0],
                      doneBlock: {
                        picker, indexes, values in


                        print("values = \(values)")
                        print("indexes = \(indexes)")
                        print("picker = \(picker)")

                        DispatchQueue.main.async {
                            // Update UI
                            self.performSegue(withIdentifier: "pdfsegue", sender: nil)
                        }
                        return
}, cancel:
    {
        ActionMultipleStringCancelBlock in return

}, origin: sender)

您现在将在Emirate数组中获得选定的values类对象。

N-B-此代码仅是让您了解其工作方式。