我正在尝试使用数组进行过滤搜索并使用搜索控制器

时间:2016-09-28 08:13:17

标签: ios swift uisearchcontroller

func filterContentForSearchText(SearchText : String, scope:String = "All"){
        let resultPredicate = NSPredicate(format: "job_title contains[cd] %@", SearchText)

        let resultJobs = SavedJobs.filteredArrayUsingPredicate(resultPredicate)
        FilteredJobs = NSMutableArray(array: resultJobs)
        FilterResults = FilteredJobs.filter{
            FilterJobs in
             return FilterJobs.name.lowercaseString.containsString(SearchText.lowercaseString)
        }
        self.tableView.reloadData()

    }

这是我filterContentForSearchText的功能,它工作正常,我唯一的问题是,当我点击搜索栏时,它返回空数组,应用程序终止说索引0超出数组边界,因为数组是我的numberOfRowsAtIndexPath位于

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

        if SearchController.active && SearchController.searchBar.text != ""{
            return FilterResults.count ?? 0

        }

        return SavedData.count ?? 0


}

我的cellForAtIndexPath位于

之下
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("SavedCell", forIndexPath: indexPath) as! SavedJobsCell

        if SearchController.active && SearchController.searchBar != ""{
            if let rowData: NSDictionary = FilteredJobs[indexPath.row] as? NSDictionary{
                cell.CompanyName.text = rowData["company_name"] as? String//FilteredJobs.valueForKey("company_name") as? String
                cell.Location.text = rowData["location"] as? String//FilteredJobs.valueForKey("location") as? String
                cell.Date.text = rowData["created_date"] as? String//FilteredJobs.valueForKey("created_date") as? String
                cell.JobTitle.text = rowData["job_title"] as? String//FilteredJobs.valueForKey("user_logo") as? String
                if let image = rowData["user_logo"] as? String{
                    let ImageUrl = NSURL(string: ("http://dev.jobsmarket.pk/uploads/user-logo/"+image))
                    cell.ComapnyIcon.sd_setImageWithURL(ImageUrl)
                }
            }
        }
        else{
        let PremiumJob = SavedData[indexPath.row]
        cell.CompanyName.text = PremiumJob.valueForKey("company") as? String
        cell.JobTitle.text = PremiumJob.valueForKey("title") as? String
        cell.Location.text = PremiumJob.valueForKey("location") as? String
        cell.Date.text = PremiumJob.valueForKey("date") as? String
        var ImageName = String()
        if let image = PremiumJob.valueForKey("user_logo") as? String{
            ImageName = "http://dev.jobsmarket.pk/uploads/user-logo/"+image
        }
        let ImageUrl = NSURL(string: ImageName)
        cell.ComapnyIcon.sd_setImageWithURL(ImageUrl, placeholderImage: UIImage(named: "JobAvatar"), options: [])
        }
        return cell
    }

我正在使用谓词,当我在搜索栏上选项卡时,它第一次给我空数组,通过哪个表接收空数组并抛出错误

0 个答案:

没有答案