0

I have one custom cell which have some name label, address label and one call button

Now what i need is, in my table view cell i have one call button . when i click that i need to get the respective row ( i.e each cell have some name and address label and one button. when i tap call button its should call the appropriate number and should show in my phone dialpad)

my customcell classs:

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) {
    }

my 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 
    }

Now how i call the number from json and how i will make a call in dial pad when i tap call button in my custom cell

Please help me out !

user5513630
  • 1,709
  • 8
  • 24
  • 48

2 Answers2

1

Have a variable in premiumUsercell like as follows,

class premiumUsercell: UITableViewCell {
    var phoneNumber = ""
}

in cellForRowAtIndexPath add this code,

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

in premiumUsercell, CallbuttonTap method

@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!)
        }
    }
Venk
  • 5,949
  • 9
  • 41
  • 52
0

you can get the row number by getting the cell object from the button clicked. once you get the row number you can get the respective phone number from you json object.

UIApplication.sharedApplication().openURL(NSURL(string: @"tel:<your telephone number>"))
Ujjwal Khatri
  • 716
  • 1
  • 6
  • 19
  • i haven't use this call function. if you give some sample code ,that will be much help full. I am struck for past 5 hours – user5513630 Mar 23 '16 at 09:48
  • since your are getting "This app is not allowed to query for scheme tel" error, could you please check this URL for solution : http://stackoverflow.com/questions/32722638/this-app-is-not-allowed-to-query-for-scheme-cydia-ios9-error – Ujjwal Khatri Mar 23 '16 at 09:54