【问题标题】:find uitableviewcell which is swiped / in editing mode找到 uitableviewcell 被刷/处于编辑模式
【发布时间】:2015-01-12 14:40:32
【问题描述】:

我在我的应用中添加了一个 UITableView。此表格视图包含单元格,如果它们向左滑动,则会显示一些自定义编辑按钮(通过此函数:editActionsForRowAtIndexPath)。我想知道哪个 UITableViewCell 向左滑动或显示自定义编辑选项并将默认文本设置为该单元格中的 detailText 标签。

进一步解释:

我有几个 UITableViewCells。用户滑动这些单元格之一。现在单元格处于编辑模式并显示我添加的编辑选项(名为“日期”)。用户按下此按钮/选项(日期)。现在一个 datePicker 正在弹出。用户选择了一个日期。这个日期写在标签上。这个 dateLabel 应该被传输到被刷过的单元格的 detailTextLabel 中。问题是我不知道如何将带有日期的标签传输到被刷过的单元格的 detailTextLabel。

我希望有人可以帮助我。我正在使用 Swift 编码。

如果 UITableViewCell 向左滑动,这是我的自定义选项代码:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {

  let editingCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!

  let date = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Date", handler: { (action, indexPath) -> Void in

  self.showSideBar(true)
  self.delegate?.sideBarWillOpen?()
  self.isEditingStyle = true

  })

  date.backgroundColor = UIColor(red: 0.204, green:0.667, blue:0.863, alpha: 1.0);

  return [date];
}

日期选择器:

    func datePickerChanged(datePicker:UIDatePicker) {

    var dateFormatter = NSDateFormatter()

    dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
    dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle

    var strDate = dateFormatter.stringFromDate(datePicker.date)
    dateLabel.text = strDate

}

在detailTextLabel中添加日期:

        lastEditedCell?.detailTextLabel?.text = dateLabel

【问题讨论】:

  • 使用 viewController 中的引用来跟踪滑动单元格:weak var lastCellEdited:UITableViewCell?

标签: ios uitableview swift xcode6


【解决方案1】:

您可以在tableView(tableView:, editActionsForRowAtIndexPath indexPath:)函数中跟踪lastEditedCell

var lastEditedCell:UITableViewCell? // property of the class

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {

  let editingCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!

   lastEditedCell = editingCell
   //Your Code

}

在选择器视图设置日期后,您可以在回调函数中设置。

lastEditedCell?.detailTextLabel.text = date

【讨论】:

  • 非常感谢您的回答。我将代码实现到我的项目中。但是不能将日期变量(dateLabel)设置为detailTextLabel的文本。我在我的问题中发布了一些代码。愿它帮助您更好地理解。您也有解决方案吗?
  • 您无法替换标签。将 detailTextLabel 的 text 属性设置为日期字符串或 datelabel.text
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多