Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 无法在tableview中单击按钮时更新日期选择器中的时间_Swift_Uitableview - Fatal编程技术网

Swift 无法在tableview中单击按钮时更新日期选择器中的时间

Swift 无法在tableview中单击按钮时更新日期选择器中的时间,swift,uitableview,Swift,Uitableview,我试图显示一个日期选择器,并在单击按钮时更新其时间 我有一个显示营业时间的表视图,如下图1所示 图1 单击计时单元格或单元格内的时间按钮时,我正在扩展单元格以显示日期选择器,我无法将日期选择器中的时间更新为时间按钮上的时间 例如,单击上午9:00时,我希望在日期选择器中显示上午9:00,如下图2所示,或者单击下午5:00时,我希望在日期选择器中显示下午5:00 此外,当用户更改日期选择器中的时间时,我希望更新tableview单元格中相应的时间按钮 图2 下面是代码,请参见下面的代码注释//

我试图显示一个日期选择器,并在单击按钮时更新其时间

我有一个显示营业时间的表视图,如下图1所示

图1

单击计时单元格或单元格内的时间按钮时,我正在扩展单元格以显示日期选择器,我无法将日期选择器中的时间更新为时间按钮上的时间

例如,单击上午9:00时,我希望在日期选择器中显示上午9:00,如下图2所示,或者单击下午5:00时,我希望在日期选择器中显示下午5:00

此外,当用户更改日期选择器中的时间时,我希望更新tableview单元格中相应的时间按钮

图2

下面是代码,请参见下面的代码注释
//更新日期选择器

因为我试图在闭包内更新日期选择器时间,所以它似乎不起作用。有人能告诉我怎么解决这个问题吗

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

        tableView.setEditing(true, animated: true)      // This displays the delete button infront of the cell
        tableView.allowsSelectionDuringEditing = true   // Allows the table view cells to be selected during editing mode

        if indexPath.section == 0 {

            let cell = Bundle.main.loadNibNamed("DailyTimesTableViewCell", owner: self, options: nil)?.first as! DailyTimesTableViewCell

            // Configure the cell...
            cell.selectionStyle             = UITableViewCellSelectionStyle.none; // Ensuring that there is no background for the selected cell
            cell.datePicker.datePickerMode  = .time         // Setting start and time            
            cell.datePicker.minuteInterval = 5
            cell.datePicker.addTarget(self, action: #selector(datePickerChanged), for: .valueChanged)

            let startTime   = timesArray[indexPath.row]["startTime"]
            cell.startTime.setTitle(startTime as! String?, for: .normal)
            cell.startTime.setTitleColor(UIColor.black, for: .normal)

            let endTime     = timesArray[indexPath.row]["endTime"]

            cell.endTime.setTitle(endTime as! String?, for: .normal)
            cell.endTime.setTitleColor(UIColor.black, for: .normal)

            // Closure
            cell.startTimeTapped = { (button  : UIButton) -> Void in

                // Updating the date picker 
                cell.datePicker.date = self.convertToDateObject(timeString: startTime as! String)
                tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)

                // Expanding the cell to show date picker 
                if self.selectedCellIndexPath != nil && self.selectedCellIndexPath == indexPath {
                    tableView.cellForRow(at: indexPath)?.setEditing(true, animated: true)   // This displays the delete button infront of the cell
                    self.selectedCellIndexPath = nil
                } else {
                    self.selectedCellIndexPath = indexPath
                }


            }

            // Closure
            cell.endTimeTapped = { (button) -> Void in
                // Updating the date picker
                cell.datePicker.date = self.convertToDateObject(timeString: endTime as! String)
                tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)

                // Expanding the cell to show date picker 
                if self.selectedCellIndexPath != nil && self.selectedCellIndexPath == indexPath{
                    tableView.cellForRow(at: indexPath)?.setEditing(true, animated: true)
                    self.selectedCellIndexPath = nil
                }else{
                    self.selectedCellIndexPath = indexPath
                }
            }
            return cell
      }
}
DailyTimesTableViewCell代码:

import UIKit

class DailyTimesTableViewCell: UITableViewCell, UITableViewDelegate {
    typealias  buttonTappedBlock = (_ button:UIButton) -> Void
    var startTimeTapped : buttonTappedBlock!
    var endTimeTapped : buttonTappedBlock!



    @IBAction func startTimeClicked(_ sender: subclassedUIButton) {
        if startTimeTapped != nil {
            startTimeTapped(sender as UIButton)
        }
    }

    @IBAction func endTimeClicked(_ sender: UIButton) {
        if endTimeTapped != nil{
            endTimeTapped(sender as UIButton)
        }
    }
}

要为
UIDatePicker
设置日期,只需使用这段代码将字符串形式的时间转换为日期

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat =  "HH:mm"
let date = dateFormatter.dateFromString("17:00")//Put textfield.text! here

datePicker.date = date
对于问题的第二部分,由于您可能有多个日期选择器,您需要做的是将选择更改侦听器添加到所有这些选择器中,然后确定更改了哪一个以及单击了哪一个标签。最后将标签设置为正确的值

您可以使用节号来确定单击了哪个数据选择器。在您的
cellForRow
中,将节添加为标记,然后添加侦听器

datePicker.tag = indexPath.section
textView.tag = indexPath.section
datePicker.addTarget(self, action: Selector("dataPickerChanged:"), forControlEvents: UIControlEvents.ValueChanged)
然后在侦听器函数中,找出哪个数据选择器更改了值

func datePickerChanged(datePicker:UIDatePicker) {
    var dateFormatter = NSDateFormatter()
    dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
    dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle

    if datePicker.tag == 0 {
        textView.text = dateFormatter.stringFromDate(datePicker.date)
    }
    // keep going

}

这部分问题的答案是:

我无法将日期选择器中的时间更新为 时间按钮

代码如下:

问题在于这行代码
tableView.reloadRows(位于:[indexath],使用:UITableViewRowAnimation.automatic)
,并通过添加
tableView.beginUpdate()
tableView.beginUpdate()
并返回单元格帮助

在函数
cellForRow At
中,更新为以下代码

cell.startTimeTapped = { (button) -> DailyTimesTableViewCell in
                self.tableView.beginUpdates()
                cell.datePicker.date = self.convertToDateObject(timeString: startTime as! String)
                if self.selectedCellIndexPath != nil && self.selectedCellIndexPath == indexPath {
                    tableView.cellForRow(at: indexPath)?.setEditing(true, animated: true)   // This displays the delete button infront of the cell
                    self.selectedCellIndexPath = nil
                } else {
                    self.selectedCellIndexPath = indexPath
                }

                tableView.endUpdates()

                return cell

            }
class DailyTimesTableViewCell: UITableViewCell, UITableViewDelegate {

typealias  buttonTappedBlock = (_ button:UIButton) -> DailyTimesTableViewCell
    var startTimeTapped : buttonTappedBlock!
    var endTimeTapped : buttonTappedBlock!
    @IBAction func startTimeClicked(_ sender: UIButton) {

        if startTimeTapped != nil {
            startTimeTapped(sender as UIButton)
        }
   }

}
在DailyTimesTableViewCell类中,更新为以下代码

cell.startTimeTapped = { (button) -> DailyTimesTableViewCell in
                self.tableView.beginUpdates()
                cell.datePicker.date = self.convertToDateObject(timeString: startTime as! String)
                if self.selectedCellIndexPath != nil && self.selectedCellIndexPath == indexPath {
                    tableView.cellForRow(at: indexPath)?.setEditing(true, animated: true)   // This displays the delete button infront of the cell
                    self.selectedCellIndexPath = nil
                } else {
                    self.selectedCellIndexPath = indexPath
                }

                tableView.endUpdates()

                return cell

            }
class DailyTimesTableViewCell: UITableViewCell, UITableViewDelegate {

typealias  buttonTappedBlock = (_ button:UIButton) -> DailyTimesTableViewCell
    var startTimeTapped : buttonTappedBlock!
    var endTimeTapped : buttonTappedBlock!
    @IBAction func startTimeClicked(_ sender: UIButton) {

        if startTimeTapped != nil {
            startTimeTapped(sender as UIButton)
        }
   }

}

谢谢您回答了问题的第二部分,但是当单击该按钮时,如何使用按钮标题“9:00AM”设置日期选择器的时间。正如您在代码中看到的,我试图在闭包内部使用这行代码
cell.datePicker.date=self.convertToDateObject(timeString:startTime as!String)
进行设置,但这似乎不起作用。(但是,当它位于闭包之外时,它也可以工作,这不是我想要的)。当我像这样在闭包内设置代码时,它不工作(即单击按钮)
cell.startTimeTapped={let dateFormatter=dateFormatter()dateFormatter.dateFormat=“HH:mm”let date=dateFormatter.date(from:“17:00”)//Put textfield.text!这里是cell.datePicker.date=date!}
@user44776是否看到错误消息?您确定选择器已正确连接到自定义单元格吗?我没有看到任何错误消息。选择器已正确连接,当在闭包外部设置相同代码时,选择器将更新,因此这将确认其已连接。将时间“17:00”分配给datePicker后,当我打印
cell.datePicker.date
时,我会得到UTC时间“2000-01-01 11:30:00+0000”,这是正确的UTC时间,但年、月和日除外。@user44776您能添加我的代码并在那里放置断点吗?您需要验证
dateFormatter
对象是否为nil。我怀疑在这样的闭包中,您失去了对单元格中对象的访问