将tableview所选数据共享到Whats App

时间:2019-09-11 12:05:10

标签: ios swift share whatsapp

我正在共享在tableview上选择的whats应用程序上的数据,因此我有两个选项来选择tableview行,一个是Select All,另一个是在didSelect上,所以当我全选时,share是完美的工作,我在这里共享编码数据是我用于共享所有表视图数据的数据

我的结构模型代码

class QuotationListDataModel: Encodable{
    var id: String?
    var quantity: String?
    var margin: String?
    var part_number: String?
    var total_price: String?
    var freight: String?

    init(id: String?,quantity: String?,margin: String?,part_number: String,total_price: String,freight: String) {
        self.id = id
        self.quantity = quantity
        self.margin = margin
        self.part_number = part_number
        self.total_price = total_price
        self.freight = freight
    }
}

这是我要获取数据的api表单

var quotationSeelctedData = [QuotationListDataModel]()

func quotationListAPI(){
    let preferences = UserDefaults.standard
    let uid = "u_id"
    let acTkn = "acc_tkn"

    let u_ID = preferences.object(forKey: uid)
    let A_Token = preferences.object(forKey: acTkn)

    let params = ["user_id": u_ID!, "access_token": A_Token!]
    print(params)
    self.viewMainSpinner.isHidden = false
    self.viewInnerSpinner.startAnimating()
    Alamofire.request(quatationlist, method: .post, parameters: params).responseJSON(completionHandler: {(response) in
        switch response.result{
        case.success(let value):
            let json  = JSON(value)
            print(json)
            let data = json["quation_list"]
            print(data)
            if data == []{
                self.viewMainSpinner.isHidden = true
                self.viewInnerSpinner.stopAnimating()
            }else{
                data.array?.forEach({ (qList) in
                    let id = qList["id"].stringValue
                    let quantity = qList["quantity"].stringValue
                    let margin = qList["margin"].stringValue
                    let part_number = qList["part_number"].stringValue
                    let total_price = qList["total_price"].stringValue
                    let freight = qList["freight"].stringValue
                    self.quotationSeelctedData.append(QuotationListDataModel(id: String(describing: id), quantity: String(describing: quantity), margin: String(describing: margin), part_number: String(describing: part_number), total_price: String(describing: total_price), freight: String(describing: freight)))
                })
                self.tblListView.reloadData()
                self.viewMainSpinner.isHidden = true
                self.viewInnerSpinner.stopAnimating()
            }
        case.failure(let error):
            print(error.localizedDescription)
            self.viewMainSpinner.isHidden = true
            self.viewInnerSpinner.stopAnimating()
        }

    })
}

所以我将所有数据存储在quotationSeelctedData中并进行如下编码

    let encoder = JSONEncoder()
    encoder.outputFormatting = .prettyPrinted
    do {
        let jsonData = try encoder.encode(quotationSeelctedData)
        if let jsonString = String.init(data: jsonData, encoding: .utf8) {
            print(jsonString)
            let stringwithoutquotes = jsonString.replacingOccurrences(of: "{", with: "")
            let removebracket1 = stringwithoutquotes.replacingOccurrences(of: "}", with: "")
            let removebracket2 = removebracket1.replacingOccurrences(of: "[", with: "")
            let removebracket3 = removebracket2.replacingOccurrences(of: "]", with: "")
            self.jsString = removebracket3
        }
    } catch {
        print("the encoding failed")
    }

我用高位代码对存储在quotationSeelctedData数组中的数据进行编码

现在,当在表视图中没有选择任何数据并且使用didSelect单击时,我仅选择一个数据并仅共享选择的数据,所以请帮助我如何仅将选择的数据共享到whats应用程序

我的意思是说,只是想共享选定的数据,所以请告诉我如何将数据存储在没有选择的数组中

我正在像下面的应用程序中共享数据

  "quantity" : "1000000000",
    "id" : "14",
    "freight" : "999",
    "total_price" : "422730",
    "margin" : "999",
    "part_number" : "15"
  ,

    "quantity" : "123456789123456789",
    "id" : "13",
    "freight" : "123456789123456789",
    "total_price" : "52965736795102108",
    "margin" : "123456789123456789",
    "part_number" : "15"
  ,

    "quantity" : "150",
    "id" : "12",
    "freight" : "15",
    "total_price" : "4186",
    "margin" : "4",
    "part_number" : "15"
  ,

    "quantity" : "150",
    "id" : "11",
    "freight" : "15",
    "total_price" : "4186",
    "margin" : "4",
    "part_number" : "15"
  ,

    "quantity" : "100",
    "id" : "10",
    "freight" : "10",
    "total_price" : "3927.0",
    "margin" : "2",
    "part_number" : "15"
  ,

    "quantity" : "12",
    "id" : "9",
    "freight" : "15",
    "total_price" : "4105.5",
    "margin" : "2",
    "part_number" : "19"
  ,

    "quantity" : "100",
    "id" : "8",
    "freight" : "10",
    "total_price" : "3927.0",
    "margin" : "2",
    "part_number" : "17"
  ,

    "quantity" : "100",
    "id" : "7",
    "freight" : "10",
    "total_price" : "3927.0",
    "margin" : "2",
    "part_number" : "15"
  ,

    "quantity" : "150",
    "id" : "6",
    "freight" : "101",
    "total_price" : "15547.35",
    "margin" : "121",
    "part_number" : "20"
  ,

    "quantity" : "100",
    "id" : "5",
    "freight" : "10",
    "total_price" : "3927.0",
    "margin" : "2",
    "part_number" : "19"
  ,

    "quantity" : "100",
    "id" : "4",
    "freight" : "10",
    "total_price" : "0.0",
    "margin" : "2",
    "part_number" : "17"
  ,

    "quantity" : "100",
    "id" : "3",
    "freight" : "10",
    "total_price" : "3927.0",
    "margin" : "2",
    "part_number" : "20"
  ,

    "quantity" : "100",
    "id" : "2",
    "freight" : "10",
    "total_price" : "3927.0",
    "margin" : "2",
    "part_number" : "15"
  ,

    "quantity" : "2",
    "id" : "1",
    "freight" : "20",
    "total_price" : "1230",
    "margin" : "30",
    "part_number" : "15"

1 个答案:

答案 0 :(得分:0)

let yourDataModel = [QuotationListDataModel]

从选定的tableViewCell中获取数据:

    func tableView(_ tableView: UITableView, 
         didSelectRowAt indexPath: IndexPath){

    let dataToBeSent = yourDataModel[indexPath.row] 

    // share this data in whatsApp using UIActivityViewController:

    let items = [dataToBeSent]
    let activityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)

    present(activityViewController, animated: true)

  }