如何从json中获取数字,并在我的自定义表格视图单元格中的用户选项卡调用按钮时在拨号盘中显示该数字

时间:2016-03-23 09:32:36

标签: ios json swift uitableview

我有一个自定义单元格,其中有一些name labeladdress label和一个call button

现在我需要的是,在我的表格视图中我有一个call button。当我点击我需要获得相应的行(即每个单元格都有一些名称和地址标签和一个按钮。当我点击通话按钮时,它应拨打相应的号码,并应显示在我的手机拨号盘中)

我的customcell类:

class premiumUsercell: UITableViewCell {


    @IBOutlet weak var vendorName3: UILabel!



    @IBOutlet weak var vendorAdddress3: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }



    @IBAction func CallbuttonTap(sender: AnyObject) {
    }

我的viewcontroller:

 func jsonParsingFromURL () {
        let url = NSURL(string: "url")
        let session = NSURLSession.sharedSession()

        let request = NSURLRequest(URL: url!)
        let dataTask = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
            // print("done, error: \(error)")

            if error == nil
            {

                dispatch_async(dispatch_get_main_queue()){
                    self.arrDict=(try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSMutableArray

                    //print(self.arrDict)

                    if (self.arrDict.count>0)
                    {
                        self.Resultcount.text = "\(self.arrDict.count) Results"
                        self.tableView.reloadData()
                    }}

                // arrDict.addObject((dict.valueForKey("xxxx")
            }


        }
        dataTask.resume()


    }




    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return self.arrDict.count
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return cellSpacingHeight
    }




    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView()
        headerView.backgroundColor = UIColor.clearColor()
        return headerView
    }
//
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        if(isTapped == true && indexPath == selectedIndex)
        {



            if (premiumUserCheck && indexPath == selectedIndex ) {

                let cell1:premiumUsercell = self.tableView.dequeueReusableCellWithIdentifier("cell3") as! premiumUsercell


                cell1.vendorName3.text=arrDict[indexPath.section] .valueForKey("name") as? String
                cell1.vendorAdddress3.text=arrDict[indexPath.section] .valueForKey("address") as? String

                print("premium user")

                return cell1


            }
            else {

                let cell1:ExpandCell = self.tableView.dequeueReusableCellWithIdentifier("cell2") as! ExpandCell


                cell1.VendorName.text=arrDict[indexPath.section] .valueForKey("name") as? String
                cell1.vendorAdress.text=arrDict[indexPath.section] .valueForKey("address") as? String
                //cell1.externalView.hidden = true

                print("non premium user") 
                return cell1 
            } 
        } 





        let cell:customCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! customCell 


        cell.vendorName.text=arrDict[indexPath.section] .valueForKey("name") as? String 
        cell.vendorAddress.text=arrDict[indexPath.section] .valueForKey("address") as? String 

        print("norml user") 



        return cell 
    }

现在,当我点击自定义单元格中的通话按钮时,我如何拨打json中的号码以及如何使用拨号盘拨打电话

请帮帮我!

2 个答案:

答案 0 :(得分:1)

premiumUsercell中有一个变量,如下所示,

class premiumUsercell: UITableViewCell {
    var phoneNumber = ""
}

cellForRowAtIndexPath中添加此代码,

cell1.phoneNumber = arrDict[indexPath.section] .valueForKey("phone") as? String

premiumUsercellCallbuttonTap方法

@IBAction func CallbuttonTap(sender: AnyObject) {

        var phoneString = "tel://\(phoneNumber)"
        let openURL = NSURL(string:phoneString)
        if openURL == nil {
            return
        }
        let application:UIApplication = UIApplication.sharedApplication()
        if (application.canOpenURL(openURL!)) {
            application.openURL(openURL!)
        }
    }

答案 1 :(得分:0)

您可以通过单击按钮获取单元格对象来获取行号。一旦你得到行号,你可以从你的json对象获得相应的电话号码。

EqId