Ios 如何在tableview中选择其他行时禁用这些行?
我只想选择特定的部分,这意味着假设选择了第1部分的行,如果我选择了第2部分的任何行,则删除第1部分的行 如何从var selectedgredients:Set=[]中获取行值 SectionTableviewController.swiftIos 如何在tableview中选择其他行时禁用这些行?,ios,swift,uitableview,checkmark,Ios,Swift,Uitableview,Checkmark,我只想选择特定的部分,这意味着假设选择了第1部分的行,如果我选择了第2部分的任何行,则删除第1部分的行 如何从var selectedgredients:Set=[]中获取行值 SectionTableviewController.swift // // SectionTableViewController.swift // SectionTableSearchDemo // import UIKit class SectionTableViewController: UITableV
//
// SectionTableViewController.swift
// SectionTableSearchDemo
//
import UIKit
class SectionTableViewController: UITableViewController, UISearchResultsUpdating,UISearchBarDelegate, UISearchControllerDelegate {
var selectedIngredients : Set<IndexPath> = []
var searchedSkillsResults = [String]()
// var objectFilter = [Skill]() //
var filteredObjects = [[Skill]]()
var products : [Category] = []
// var skillCat : [Skill] = []
//var catSkill : Category?
var isSearching = false
var cat : Welcome?
var cate : Category?
var searchController : UISearchController!
var filterData : [String]!
override func viewDidLoad() {
super.viewDidLoad()
parseJSON()
searchControllerSetup()
}
private func searchControllerSetup(){
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.delegate = self
navigationItem.searchController = searchController
searchController.obscuresBackgroundDuringPresentation = false
navigationItem.hidesSearchBarWhenScrolling = false
}
// i don't know how to implement and show only skills when search
func updateSearchResults(for searchController: UISearchController) {
let searchString = searchController.searchBar.text!
if searchString.isEmpty {
filteredObjects.removeAll()
}else{
let filterObjects = (cat?.category.map({
$0.skill.filter({$0.vName.lowercased().contains(searchString.lowercased())})
}).filter({ $0.count > 0 }))!
filteredObjects = filterObjects
print("filter data count is in search before \(filteredObjects.count)")
// print("filter data is \(filteredObjects)")
}
if searchController.isActive {
isSearching = true
}else{
isSearching = false
}
tableView.reloadData()
}
private func parseJSON(){
guard let path = Bundle.main.path(forResource: "category", ofType: "json")else{
return
}
let url = URL(fileURLWithPath: path)
do {
let jsonData = try Data(contentsOf: url)
cat = try JSONDecoder().decode(Welcome.self,from: jsonData)
if let result = cat {
products = result.category
// print(result)
}
} catch {
print("Error is \(error)")
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
if isSearching && searchController.searchBar.text != "" {
return filteredObjects.count //final
}else{
return products.count
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearching && searchController.searchBar.text != ""{
return filteredObjects[section].count //final
}else{
return cat!.category[section].skill.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if isSearching && searchController.searchBar.text != "" {
let text = filteredObjects[indexPath.section][indexPath.row]
cell.textLabel?.text = text.vName
}else{
let text = cat?.category[indexPath.section].skill[indexPath.row].vName
cell.textLabel?.text = text
}
if selectedIngredients.contains(indexPath){
cell.accessoryType = .checkmark
}else{
cell.accessoryType = .none
}
cell.selectionStyle = .none
return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let titleText = cat?.category[section].vName
return titleText
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if self.selectedIngredients.contains(indexPath){
self.selectedIngredients.remove(indexPath)
}else{
self.selectedIngredients.insert(indexPath)
}
self.tableView.reloadData()
}
}
//
//SectionTableViewController.swift
//SECTIONTABLESEARCDEMO
//
导入UIKit
类节TableViewController:UITableViewController、UISearchResultsUpdated、UISearchBarDelegate、UISearchControllerDelegate{
变量selectedgredients:Set=[]
var searchedSkillsResults=[String]()
//var objectFilter=[Skill]()//
变量filteredObjects=[[Skill]]()
var产品:[类别]=[]
//var skillCat:[技能]=[]
//卡茨基尔:类别?
var isSearching=false
猫:欢迎你?
类别?
var searchController:UISearchController!
变量filterData:[字符串]!
重写func viewDidLoad(){
super.viewDidLoad()
parseJSON()
searchControllerSetup()
}
专用函数searchControllerSetup(){
searchController=UISearchController(searchResultsController:nil)
searchController.SearchResultsUpdate=self
searchController.delegate=self
navigationItem.searchController=searchController
searchController.obscuresBackgroundDuringPresentation=false
navigationItem.HideseArchBarWhenScrolling=false
}
//我不知道如何在搜索时实现和显示技能
func updateSearchResults(对于searchController:UISearchController){
让searchString=searchController.searchBar.text!
如果searchString.isEmpty{
filteredObjects.removeAll()
}否则{
设FilterObject=(cat?.category.map({
$0.skill.filter({$0.vName.lowercased().contains(searchString.lowercased())})
}).filter({$0.count>0}))!
filteredObjects=filterObjects
打印(“筛选数据计数在\(filteredObjects.count)之前的搜索中”)
//打印(“筛选数据为\(筛选对象)”)
}
如果searchController.isActive{
isSearching=true
}否则{
isSearching=false
}
tableView.reloadData()
}
private func parseJSON(){
guard let path=Bundle.main.path(forResource:“category”,of type:“json”)else{
返回
}
让url=url(fileURLWithPath:path)
做{
让jsonData=try数据(contentsOf:url)
cat=try JSONDecoder().decode(Welcome.self,from:jsonData)
如果让结果=cat{
产品=结果。类别
//打印(结果)
}
}抓住{
打印(“错误为\(错误)”)
}
}
//标记:-表视图数据源
重写func numberOfSections(在tableView:UITableView中)->Int{
如果正在搜索(&searchController.searchBar.text!“”{
返回filteredObjects.count//final
}否则{
退货数量
}
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
如果正在搜索(&searchController.searchBar.text!“”{
返回filteredObjects[节]。计数//最终
}否则{
返回cat!.category[section].skill.count
}
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(带有标识符:“cell”,用于:indexath)
如果正在搜索(&searchController.searchBar.text!“”{
让text=filteredObjects[indexPath.section][indexPath.row]
cell.textlab?.text=text.vName
}否则{
设text=cat?.category[indexPath.section].skill[indexPath.row].vName
cell.textLabel?.text=文本
}
如果选择了greedients.contains(indexPath){
cell.accessoryType=.checkmark
}否则{
cell.accessoryType=.none
}
cell.selectionStyle=.none
返回单元
}
重写func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String{
设titleText=cat?.category[section].vName
返回标题文本
}
重写func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){
如果self.selectedIngredients.contains(indexPath){
self.selectedgredients.remove(indexPath)
}否则{
self.selectedgredients.insert(indexPath)
}
self.tableView.reloadData()
}
}
看起来像
请忽略其他不相关的代码
帮帮我或任何提示帮帮我或任何提示,
非常感谢。一组索引路径是不好的做法。在数据模型中保持
选中状态。然后更新模型并重新加载表视图。@vadian我知道,但我们必须使用它,您知道如何打印复选标记的行值(我的意思是名称)