【问题标题】:Edit button for deleting rows in UITableView in Swift is not working用于在 Swift 中删除 UITableView 中的行的编辑按钮不起作用
【发布时间】:2015-04-15 20:02:37
【问题描述】:

我有一个带有自定义单元格类和导航控制器的表格。我向导航控制器添加了一个编辑按钮,并为视图控制器创建了一个 IBAction:

@IBAction func doEdit(sender: AnyObject) {  
    self.tableView.setEditing(true, animated: true)
}

Xcode 告诉我:

Cannot invoke 'setEditing' with an argument list of type '(Bool, animated: Bool)'

我也在 viewDidLoad 方法中尝试了navigationItem.rightBarButtonItem = editButtonItem(),但是那里没有显示删除图标。

编辑:我为 UITableView 创建了一个名为 table 的插座。当输入self.table.setEditing(true, animated: true) 时,上述错误不再出现,但应用程序在单击编辑按钮时崩溃。代码如下:

import UIKit

var arr = ["1", "2", "3"]

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var table: UITableView!

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arr.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: CustomCellClass = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCellClass   
    cell.cellTitle.text = arr[indexPath.row]   
    cell.backgroundColor = UIColor(red: 242.0/255.0, green: 157.0/255.0, blue: 48.0/255.0, alpha: 1.0)     
    return cell   
}

@IBOutlet weak var editButton: UIBarButtonItem!

@IBAction func doEdit(sender: AnyObject) {
    self.table.setEditing(true, animated: true)
}

override func viewDidLoad() {
    super.viewDidLoad()       
    // navigationItem.rightBarButtonItem = editButtonItem()     
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewDidAppear(animated: Bool) {
    self.table.reloadData()
} 
}

【问题讨论】:

  • 您的“tableView”是否作为IBOutlet 连接到该视图控制器中的表视图?
  • 是的,它被称为“表”。请参阅上面的更新问题。

标签: ios uitableview swift


【解决方案1】:

确保您的 tableView 从Interface Builder 连接为IBOutlet

您可以通过从 IB 按住 ctrl 拖动到您的代码来做到这一点。

【讨论】:

    【解决方案2】:

    现在可以使用了,我删除了该插座并创建了一个名为tableView 的新插座。

    【讨论】:

      猜你喜欢
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多