2016-11-07 74 views
0

所以我有一個自定義tableview,並在該tableview中我有一個按鈕,圖像和2個標籤。這些項目中的每一個都會使用php從mysql服務器獲取json數據。我將我的項目從Objective-C轉換爲Swift,這樣我得到了這個錯誤。這是我的項目中最重要的代碼之一,因爲用戶點擊按鈕後,它將數組移動到其他數組,這樣做會爲單元格提供一個特殊的行號,以便所有圖像,標籤和按鈕知道哪個是要顯示的。Swift Error convertPoint(CGPoint,到:UITableView)

我試過切換它,所以.convert()但只是錯誤,所以我離開它是如何它原來。

爲按鈕

// 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() 

} 
} 

錯誤的代碼

error picture

錯誤與新代碼

圖片所以它更容易閱讀

error

error 3

error 4

回答

3

convertPoint在夫特3被改變等convert(_:to:)

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

檢查Apple Documentation瞭解更多詳情。

編輯:

爲了您的第一次警告,而不是usinf indexPath != nil你需要使用if let併爲標籤錯誤,你需要轉換爲customCell你的,你正在使用。

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 
} 

爲了您deleteRows錯誤,你需要指定UITableViewRowAnimationtruefalse

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

有關UITableViewRowAnimation檢查文檔的更多詳情。

+0

我在做這件事時得到3個錯誤給代碼 – BroSimple

+0

我在底部加了他們 – BroSimple

+0

謝謝兄弟,一切都很完美 – BroSimple