【问题标题】:Search Bar Controller with Table view in swift带有表格视图的快速搜索栏控制器
【发布时间】:2017-12-28 07:18:43
【问题描述】:

我是 Swift 编程新手

带有 tableview 的 SearchBar 控制器。我想在表格视图中显示多个学生,它工作正常。我可以将搜索栏控制器添加到表格视图并显示特定的学生数据。表格视图单元格包含我想要在搜索后显示特定学生的学生信息和图像

这是代码

@IBOutlet var SearchBarDisp:UISearchBar!

override func viewDidLoad()
 {
        super.viewDidLoad()
        searchController.searchResultsUpdater = self
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.dimsBackgroundDuringPresentation = false
        tableView.tableHeaderView = searchController.searchBar  
   }

func updateSearchResults(for searchController: UISearchController) {
//todo
}

我可以从json获取学生数据

func getKids(url : String) {

        UIApplication.shared.beginIgnoringInteractionEvents()

        var errorCode = "1"

        var msg = "Failed"

        var request = URLRequest(url: URL(string: "getstaffstudents",
                                          relativeTo: URL(string: serverURL+"/rkapi/api/"))!)

        let session = URLSession.shared

        request.httpMethod = "POST"

        let bodyData = "staffId=\(staffId)"

        request.httpBody = bodyData.data(using: String.Encoding.utf8);

        let task = session.dataTask(with:request,completionHandler:{(d,response,error)in

            do{

                if let data = d {

                    if let jsonData = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary {

                        errorCode = String(describing: jsonData["errorCode"]!)

                        msg = jsonData["msg"] as! String

                       print("msg values",msg)

                        if errorCode == "0" {

                            if let kid_list = jsonData["students"] as? NSArray {

                                for i in 0 ..< kid_list.count {

                                    if let kid = kid_list[i] as? NSDictionary {

                                         kidHoleData.removeAll()

                                        let imageURL = url+"/images/" + String(describing: kid["photo"]!)

                                        self.kidsData.append(Kids(
                                            studentId: kid["studentId"] as? String,
                                            name:kid["name"] as? String,
                                            classId : kid["classId"] as? String,
                                            standard: ((kid["standard"] as? String)! + " " + (kid["section"] as? String)!),
                                            photo : (imageURL),
                                            school: kid["schoolName"] as? String,
                                            schoolId : "1",
                                            url : url)
                                        )
                                    }


                                }
                                self.loopCount += 1

                                self.do_table_refresh()
                             }

                        } else {
                            self.displayAlert("Kids", message: msg)
                        }

                    } else {

                        self.displayAlert("Kids", message: "Data Not Available. Please try again")

                    }
                }else {

                    self.displayAlert("Kids", message: "Please try again")

                }

            } catch let err as NSError {

                print("JSON Error \(err)")
            }
        })

        task.resume()
    }

表格视图

 func numberOfSections(in tableView: UITableView) -> Int {

        return (kidsData.count == 0) ? 0 : 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return kidsData.count


    }


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

        let cell =
            tableView.dequeueReusableCell(
                withIdentifier: "Kidscell", for: indexPath) as! KidsTableViewCell

        let maskLayer = CAShapeLayer()
        let bounds = cell.bounds

        maskLayer.path = UIBezierPath(roundedRect: CGRect(x: 3, y: 3, width: bounds.width-15, height: bounds.height-15), cornerRadius: 2).cgPath
        cell.layer.mask = maskLayer

        let row = (indexPath as NSIndexPath).row
        if(kidsData.count>0){

            let kid = kidsData\[row\] as Kids
            cell.kidNameLabel.text = kid.name
            cell.classLabel.text = kid.standard
            cell.kidNameLabel.lineBreakMode = .byWordWrapping
            cell.kidNameLabel.numberOfLines = 0
            cell.kidImageView.image = UIImage(named: "profile_pic")
            cell.kidImageView.downloadImageFrom(link: kid.photo!, contentMode: UIViewContentMode.scaleAspectFit)  //set your image from link array.

        }
        return cell

    }

我想在表格视图中显示特定学生(例如,如果我可以将学生姓名搜索为 vani,它会在表格视图中显示学生 vani)请帮助我

【问题讨论】:

    标签: ios swift uitableview swift3 swift2


    【解决方案1】:

    将洞数据复制到出勤信息复制

     var attendanceInfoDupilicate = [AttendanceInfo]()
    

    Json 服务调用内部结束

    self.attendanceInfoDupilicate = self.attendanceInfo
    

    更新搜索控制器

        func updateSearchResults(for searchController: UISearchController)
        {
    
    
    
            let searchToSeatch = AttendanceSearchBarController.searchBar.text
    
            if(searchToSeatch == "")
            {
                self.attendanceInfo = self.attendanceInfoDupilicate
    
            }
            else{
    
                self.attendanceInfo.removeAll()
    
                let AttedanceData = self.attendanceInfoDupilicate
    
                var kidsArray = [String]()
    
                for AttendanceInfo in AttedanceData
                {
    
                    kidsArray.append(AttendanceInfo.name)
                    if(AttendanceInfo.name.range(of: searchToSeatch!, options: .caseInsensitive) != nil)
                    {
                        self.attendanceInfo.append(AttendanceInfo)
    
                    }
    
                }
    
    
    
    
            }
    
            self.TableView.reloadData()
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多