Search code examples
swiftuitableviewswift3nsindexpathcgpoint

Swift Error convertPoint(CGPoint, to: UITableView)


So I have a custom tableview and in that tableview I have a button, an image, and 2 labels. Each of these items gets filled with json data from a mysql server using php. I converted my project from Objective-C to Swift and in doing so I got this error. This is one of the most important codes in my project because since the user clicks the follow button it moves the array to other arrays, and in doing that it gives a special row number to the cell so all the images, labels, and the button knows which is which to display.

I tried switching it so .convert() but just errors so I left it how it was orginally.

The code for the button

// Follow Button
@IBAction func followButtonClick(_ sender: Any) {

// Adding row to tag
var buttonPosition = (sender as AnyObject).convertPoint(CGPoint.zero, to: self.myTableView)
var indexPath = self.myTableView.indexPathForRow(at: buttonPosition)!

// Creating an action per tag
if indexPath != nil {

    // Showing Status Labels
    var cell = self.myTableView.cellForRow(atIndexPath: indexPath)!
    cell.firstStatusLabel.isHidden = false
    cell.secondStatusLabel.isHidden = false

    // Change Follow to Following
    (sender as AnyObject).setImage(UIImage(named: "follow.png")!, for: .normal)
    cell.followButton.isHidden = true
    cell.followedButton.isHidden = false
    self.myTableView.beginUpdates()

    // ----- Inserting Cell to Section 0 -----
    followedArray.insert(testArray[indexPath.row], at: 0)
    myTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)

    // ----- Removing Cell from Section 1 -----
    testArray.remove(at: indexPath.row)
    var rowToRemove = indexPath.row
    self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 1)], with: true)

    self.myTableView.endUpdates()

 }
}

The Error

error picture

Errors with new code

Pictures so its easier to read

error

error 3

error 4


Solution

  • convertPoint is changed in Swift 3 like convert(_:to:).

    var buttonPosition = (sender as AnyObject).convert(CGPoint.zero, to: self.myTableView)
    

    Check Apple Documentation for more details.

    Edit:

    For your first warning instead of usinf indexPath != nil you need to use if let and for that label error you need to cast your cell to customCell that you are using.

    var buttonPosition = (sender as AnyObject).convert(CGPoint.zero, to: self.myTableView)
    if let indexPath = self.tblSubscriber.indexPathForRow(at: buttonPosition) {
         //Cast your `UITableViewCell` to CustomCell that you have used
         var cell = self.myTableView.cellForRow(atIndexPath: indexPath) as! CustomCell 
    }
    

    For your deleteRows error you need to specify the UITableViewRowAnimation not true or false.

    self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 1)], with: .automatic)
    

    For more detail about UITableViewRowAnimation check documentation.