我正在尝试使用日期选择器根据日期搜索我的表视图,但是由于某种原因,该表没有更新。
目标是显示仅与从日期选择器中选择的日期匹配的单元格。 这是我的表格视图代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomTableViewCell
let client = clinetarr[indexPath.row]
var output: String = ""
var resp: String = ""
var datee: String = ""
var index: String = ""
cell.message.numberOfLines=0
if let i = client["index"] as? String {
index = "\(i) " //using space as separator
}
if let date = client["date"] as? String {
datee = "\(date) " //using space as separator
}
if let response = client["response"] as? String {
resp = "\(response) "
}
if let message = client["message"] as? String {
output = message
}
cell.indextnumber.text = index
cell.response.text = resp
cell.date.text = datee
cell.message.text = output
return cell
}
这是我的日期选择器代码和已完成的按钮功能
func createDatePicker() {
let toolbar = UIToolbar()
toolbar.sizeToFit()
let done = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donebuttom))
toolbar.setItems([done], animated: false)
dateField.inputAccessoryView = toolbar
dateField.inputView = picker
picker.datePickerMode = .date
}
@objc func donebuttom() -> UITableViewCell{
var output: String = ""
var resp: String = ""
var datee: String = ""
var index: String = ""
// format date
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let dateString = formatter.string(from: picker.date)
let indexPath = IndexPath(row: 0, section: 0)
let cell = tableView.cellForRow(at: indexPath) as! CustomTableViewCell
let client = clinetarr[indexPath.row]
dateField.text = "\(dateString)"
if let date = client["date"] as? String {
let dateee = "\(date) "
let last = dateee.prefix(10)
if last == dateString{
if let i = client["index"] as? String {
index = "\(i) "
}
if let date = client["date"] as? String {
datee = "\(date) " //using space as separator
}
if let response = client["response"] as? String {
resp = "\(response) "
}
if let message = client["message"] as? String {
output = message
}
tableView.reloadData()
}
else{
resp=""
tableView.reloadData()
}
}
cell.indextnumber.text = index
cell.response.text = resp
cell.date.text = datee
cell.message.text = output
tableView.reloadData()
self.view.endEditing(true)
return cell
}
由于某种原因,表格没有更新 预先感谢
感谢我能解决每个答案。 特别感谢Duncan C,答案很简单,但我想我的大脑被炸了。
答案 0 :(得分:0)
您不搜索表格视图。表格视图具有数据源,该数据源提供了模型中的单元格。
如果要过滤表视图,则需要使用要显示的项目子集创建一个新数组,编写数据源方法,使他们知道使用过滤后的项目列表,然后调用reloadData()在表视图上。表格视图将负责弄清有多少节和行,并显示过滤数据的单元格。
答案 1 :(得分:0)
这是我的答案:
@objc func donebuttom() {
// format date
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
dateString = formatter.string(from: picker.date)
dateField.text = "\(dateString)"
let x = dateString.count
print ("here is x \(x)")
doneclick = true
tableView.reloadData()
self.view.endEditing(true)
}
这是我的表格视图
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomTableViewCell
client = clinetarr[indexPath.row]
cell.message.numberOfLines=0
if doneclick == true{
if let date = client["date"] as? String {
let dateee = "\(date)"
let last = dateee.prefix(10)
let y = last.count
if last == dateString{
if let i = client["index"] as? String {
index = "\(i) " //using space as separator
}
if let date = client["date"] as? String {
datee = "\(date) " //using space as separator
}
if let response = client["response"] as? String {
// resp = "\(response) "
let x = "\(response) "
print ("\(x) ")
if x.contains("1"){
resp = "Present"
}
else if x.contains("2"){
resp = "Absent"
}
else if x.contains("3"){
resp = "Tardy"
}
}
if let message = client["message"] as? String {
output = message
}
}else{
index = ""
datee = ""
resp = ""
output = ""
}
}
}else{
if let i = client["index"] as? String {
index = "\(i) " //using space as separator
}
if let date = client["date"] as? String {
datee = "\(date) " //using space as separator
}
if let response = client["response"] as? String {
// resp = "\(response) "
let x = "\(response) "
print ("\(x) ")
if x.contains("1"){
resp = "Present"
}
else if x.contains("2"){
resp = "Absent"
}
else if x.contains("3"){
resp = "Tardy"
}
}
if let message = client["message"] as? String {
output = message
}
}
cell.indextnumber.text = index
cell.response.text = resp
cell.date.text = datee
cell.message.text = output
return cell
}