【问题标题】:Edit cell outlets when editing tableview编辑表格视图时编辑单元格插座
【发布时间】:2017-06-30 20:42:33
【问题描述】:

这是我的应用程序:

当我按下编辑按钮(''Rediger'')时,我得到以下信息:

所以我的问题是:如何在编辑表格视图时隐藏圆形红色圆圈和披露指示器?圆圈和显示指示器都是 imageViews。

这是自定义单元格的代码(我删除了不必要的部分):

class KundeavisCell: UITableViewCell {

@IBOutlet weak var unreadImage: UIImageView!
@IBOutlet weak var disclosureIndicatorImage: UIImageView!

}

以及带有表格的视图控制器:

class KundeaviserVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

@IBOutlet weak var redigerOutlet: UIBarButtonItem!

var stores = ["Rema 1000", "Coop Obs", "Coop Extra"]

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "KundeavisCell", for: indexPath) as! KundeavisCell

    // Here i edit the cell

    return cell
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

@IBAction func tableViewEditingPressed(_ sender: Any) {

    if tableView.isEditing {
        tableView.setEditing(false, animated: true);
        redigerOutlet.style = UIBarButtonItemStyle.plain;
        redigerOutlet.title = "Rediger";

    } else {

        tableView.setEditing(true, animated: true);
        redigerOutlet.title = "Ferdig";
        redigerOutlet.style =  UIBarButtonItemStyle.done;

    }
}

func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {

    let itemToMove = stores[sourceIndexPath.row]
    stores.remove(at: sourceIndexPath.row)
    stores.insert(itemToMove, at: destinationIndexPath.row)
}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return UITableViewCellEditingStyle.none
}

func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
    return false
}

}

我知道我可以通过设置约束来将图像向左缩进,但我想完全隐藏它们。我发现了一个关于它的问题:Link 但不幸的是它是 5 年前用 Objective-C 编写的。

任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 不幸的是,我不会用 Swift 编写代码。但是,请尝试子类化您的 UITableViewCell 并覆盖 setEditing:animated: ,如该线程中所述:stackoverflow.com/questions/19678695/… ,从那里您可以将 ImageViews 设置为 hidden = YES/NO 取决于您如何编辑/停止编辑.
  • 我想知道我们可以把编辑按钮改到右边吗?

标签: ios iphone swift uitableview


【解决方案1】:

您引用的代码到现在仍然有效。您已经有了 UITableViewCell 的自定义实现,所以现在实现 SetEditing 函数并在实现中隐藏/显示您的图像。

override func setEditing(_ editing: Bool, animated: Bool) {
    unreadImage.isHidden = editing
    disclosureIndicatorImage.isHidden = editing
}

应该可以的。

【讨论】:

  • 嘿,这很好用。非常感谢您帮助我!
  • 我想知道我们可以把编辑按钮改到右边吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多