Tag: 内联日期选择器

如何使用Swift 5在iOS中实现内联日期选择器

我们已经了解了iOS默认的日历应用及其外观。 让我们看看如何像日历应用一样从头开始实现内联日期选择器。 这是典型日历应用程序中的添加事件屏幕的外观。 我希望阅读此博客的开发人员具备使用Xcode创建新项目的能力。 因此,让我们深入研究实际的实现。 我创建了一个分组表视图,并使用setupTableView()方法设置了它的数据源和委托,如下所示。 func setupTableView(){ta​​bleView = UITableView(frame:CGRect(x:0,y:0,width:view.bounds.size.width,height:view.bounds.size.height),style:.grouped) view.addSubview(tableView)tableView.register(UINib(nibName:DateTableViewCell.nibName(),bundle:nil),forCellReuseIdentifier:DateTableViewCell.reuseIdentifier())tableView.register(UINib(nibName:DatePickerTableViewCell.nibName(),bundle:nil ),forCellReuseIdentifier:DatePickerTableViewCell.reuseIdentifier())tableView.dataSource = selftableView.delegate = self} 我们已经声明了两个主要变量。 一个,我们声明了一个字符串数组,其中包含要添加的日期选择器标题文本的数量。 我们有datePickerIndexPath,它包含表视图中当前显示的日期选择器的indexPath。 var inputTexts:[String] = [“开始日期”,“结束日期”,“另一个日期”] var datePickerIndexPath:IndexPath? 现在我们来看主体部分,数据源和委托方法。 在行数方法中,当未显示日期选择器时,我们返回inputTexts数组的计数。 如果日期选择器当前可见,我们为此添加一个额外的单元格。 func tableView(_ tableView:UITableView,numberOfRowsInSection部分:Int)-> Int {//如果datePicker已经存在,如果datePickerIndexPath!= nil {return inputTexts.count + 1}否则,我们为此添加一个额外的单元格。{返回inputTexts。 count}} func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)-> UITableViewCell {if datePickerIndexPath == indexPath {让datePickerCell = tableView.dequeueReusableCell(withIdentifier:DatePickerTableViewCell.reuseIdentifier())为! DatePickerTableViewCell datePickerCell.updateCell(date:inputDates [indexPath.row-1],indexPath:indexPath)datePickerCell.delegate =自返回datePickerCell}否则{让dateCell […]