【问题标题】:Swift Error convertPoint(CGPoint, to: UITableView)Swift 错误转换点(CGPoint,到:UITableView)
【发布时间】:2016-11-07 05:30:29
【问题描述】:

所以我有一个自定义的tableview,在那个表格视图中我有一个按钮、一个图像和 2 个标签。这些项目中的每一个都使用 php 填充来自 mysql 服务器的 json 数据。我将我的项目从 Objective-C 转换为 Swift,这样做我得到了这个错误。这是我项目中最重要的代码之一,因为由于用户单击跟随按钮,它会将数组移动到其他数组,并且这样做会为单元格提供一个特殊的行号,以便所有图像、标签和按钮知道要显示哪个。

我尝试将其切换为 .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()

 }
}

错误

新代码出错

图片更容易阅读

【问题讨论】:

    标签: swift uitableview swift3 nsindexpath cgpoint


    【解决方案1】:

    convertPoint 在 Swift 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 错误,您需要指定UITableViewRowAnimation 而不是true 或false。

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

    有关UITableViewRowAnimation的更多详细信息,请查看文档。

    【讨论】:

    • 对其余代码执行此操作时出现 3 个错误
    • 我在底部添加了它们
    • @BroSimple 欢迎朋友 :)
    【解决方案2】:

    如果您在 swift3 中尝试此代码,则上述方法将更新为 func convert(_ point: CGPoint, from view: UIView?) -> CGPoint你可以这样使用

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

    我已经在 Playground 中检查过了,它正在工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多