如何在 iOS Swift 中使用段控制从模型中过滤名称、日期?
Posted
技术标签:
【中文标题】如何在 iOS Swift 中使用段控制从模型中过滤名称、日期?【英文标题】:How to filter name, date from model using segment control in iOS Swift? 【发布时间】:2019-08-28 11:16:41 【问题描述】:我尝试从段控制中过滤数据。我有五个部分,即“全部”、“请求”、“申请者”、“总价值”、“日期”。我尝试使用
过滤值searchedData.sorted(by: $0.req! > $1.req! )
但不工作。
这是我的静态数组:
var sampleData = [RequisitionModel(req: "123", requisitioner: "eqqw", totalValue: "4123", dateCreated: "05/7/2022"),
RequisitionModel(req: "1234", requisitioner: "seqw", totalValue: "0123", dateCreated: "20/8/2022"),
RequisitionModel(req: "134", requisitioner: "efqw", totalValue: "56123", dateCreated: "10/5/2022"),
RequisitionModel(req: "3634", requisitioner: "eqw", totalValue: "6123", dateCreated: "20/9/2022"),
RequisitionModel(req: "22234", requisitioner: "deqw", totalValue: "123", dateCreated: "20/10/2022")]
这是我的段控制功能:
@IBAction func segmentedControlAction(sender: AnyObject)
switch segmentFilter.selectedSegmentIndex
case 0:
print("All")
dataFilter = 0
case 1:
print("Req No")
dataFilter = 1
case 2:
print("Requisitioner")
dataFilter = 2
case 3:
print("Total Value")
dataFilter = 3
case 4:
print("Date Created")
dataFilter = 4
default:
print("All")
dataFilter = 0
reload()
func reload()
DispatchQueue.main.async
self.tableView.reloadData()
这是我的 tableview 函数,我正在尝试过滤数据:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "prCell", for: indexPath) as! PrListCellTableViewCell
var data = searchedData[indexPath.row]
switch dataFilter
case 0:
tableView.reloadData()
case 1:
searchedData = searchedData.sorted(by: $0.req! > $1.req! )
case 2:
searchedData = searchedData.sorted(by: $0.requisitioner! > $1.requisitioner! )
case 3:
data.totalValue = searchedData[indexPath.row].totalValue
case 4:
data.dateCreated = searchedData[indexPath.row].dateCreated
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"// yyyy-MM-dd"
var convertedArray = [Date]()
for dat in searchedData
let date1 = dateFormatter.date(from: dat.dateCreated!)
if let date = date1
convertedArray.append(date)
let ready = convertedArray.sorted(by: $0.compare($1) == .orderedDescending )
print(ready)
default:
data = searchedData[indexPath.row]
print("req|:\(String(describing: data.req))")
cell.reqLbl.text = data.req
cell.requisitionerLbl.text = data.requisitioner
cell.totalValueLbl.text = data.totalValue
cell.dateCreatedLbl.text = data.dateCreated
let ChecktapGesture = UITapGestureRecognizer(target: self, action: #selector(self.checktapBtnAction(_:)))
cell.tickImageView.tag = indexPath.row
cell.tickImageView.addGestureRecognizer(ChecktapGesture)
cell.tickImageView.isUserInteractionEnabled = true
let passReqtapGesture = UITapGestureRecognizer(target: self, action: #selector(self.passReqtapBtnAction(_:)))
cell.passReqNo.tag = indexPath.row
cell.passReqNo.addGestureRecognizer(passReqtapGesture)
cell.passReqNo.isUserInteractionEnabled = true
return cell
我无法从 searchData 数组中过滤数据。任何帮助都非常感谢...
【问题讨论】:
您需要搜索数据或排序数据? 如果您需要搜索数据,那么您要搜索的字符串在哪里? @AbuUlHassan 我正在尝试根据日期、req no、requisitioner 和 totalvalue 对数据进行排序。 【参考方案1】:从 cellForRow 方法中移除切换条件并添加到 segmentedControl 动作方法中 如下所示,看看魔法
var sampleData = [RequisitionModel(req: "123", requisitioner: "eqqw", totalValue: "4123", dateCreated: "05/7/2022"),
RequisitionModel(req: "1234", requisitioner: "seqw", totalValue: "0123", dateCreated: "20/8/2022"),
RequisitionModel(req: "134", requisitioner: "efqw", totalValue: "56123", dateCreated: "10/5/2022"),
RequisitionModel(req: "3634", requisitioner: "eqw", totalValue: "6123", dateCreated: "20/9/2022"),
RequisitionModel(req: "22234", requisitioner: "deqw", totalValue: "123", dateCreated: "20/10/2022")]
@IBAction func segmentedControlAction(sender: AnyObject)
switch segmentFilter.selectedSegmentIndex
case 0:
searchData = sampleData
break
case 1:
searchedData = searchedData.sorted(by: $0.req! > $1.req! )
case 2:
searchedData = searchedData.sorted(by: $0.requisitioner! > $1.requisitioner! )
break
case 3:
searchedData = $0. totalValue! > $1. totalValue!
break
case 4:
data.dateCreated = searchedData[indexPath.row].dateCreated
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"// yyyy-MM-dd"
let searchedData = searchedData r.sorted dateFormatter.date(from: $0. dateCreated!)! > dateFormatter.date(from: $1. dateCreated!)!
print(ready)
break
default:
searchData = sampleData
tableView.reloadData()
unc tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "prCell", for: indexPath) as! PrListCellTableViewCell
var data = searchedData[indexPath.row]
print("req|:\(String(describing: data.req))")
cell.reqLbl.text = data.req
cell.requisitionerLbl.text = data.requisitioner
cell.totalValueLbl.text = data.totalValue
cell.dateCreatedLbl.text = data.dateCreated
let ChecktapGesture = UITapGestureRecognizer(target: self, action: #selector(self.checktapBtnAction(_:)))
cell.tickImageView.tag = indexPath.row
cell.tickImageView.addGestureRecognizer(ChecktapGesture)
cell.tickImageView.isUserInteractionEnabled = true
let passReqtapGesture = UITapGestureRecognizer(target: self, action: #selector(self.passReqtapBtnAction(_:)))
cell.passReqNo.tag = indexPath.row
cell.passReqNo.addGestureRecognizer(passReqtapGesture)
cell.passReqNo.isUserInteractionEnabled = true
return cell
【讨论】:
在分段控制操作中我没有 data.totalValue = searchedData[indexPath.row].totalValue indexpath。 它显示错误实例成员“行”不能用于类型“索引路径”以上是关于如何在 iOS Swift 中使用段控制从模型中过滤名称、日期?的主要内容,如果未能解决你的问题,请参考以下文章
pytorch-模型训练中过拟合和欠拟合问题。从模型复杂度和数据集大小排查问题