我想使用UISearchBar在TableView中搜索

时间:2018-12-27 14:12:44

标签: xcode swift4.2

我有一个大的视频图像,然后是上传视频的用户图像以及“表格视图”单元格中的视频标题和说明。使用搜索器,我正在搜索,但是只有标题更改了视频图像描述等仍属于第一个索引,请提供帮助。 这是代码。 我使用alamofire将数据带入3个数组Video_User,Video_Image, Video_Name并在Video_Name的基础上进行搜索。

@IBOutlet weak var tableview: UITableView!
var Video_User = [String]()
var Video_Image = [String]()
var Video_Name = [String]()
var img = ""
var name = ""
var pass = ""
var searchingvideos = [String]()
var searching = false

override func viewDidLoad()
{
    super.viewDidLoad()

    getData()
}

func getData()
{
    let urlString = "\(AppDelegate.url)get_vid"


    Alamofire.request(urlString, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON
        {
            response in
            print(response.request as Any)
            print(response.response as Any)
            print(response.data as Any)
            print(response.result as Any)

            switch response.result
            {
            case .success:
                if let JSON = response.result.value
                {
                    let dlc = (JSON as! NSDictionary)
                    print(dlc)
                    let outPut = dlc.value(forKey: "get_vidResult") as! NSArray
                    for item in outPut
                    {
                        let tempitem = item as! NSDictionary
                        print(tempitem)
                        self.Video_Name.append(tempitem.value(forKey: "VideoName") as! String)
                        self.Video_User.append(tempitem.value(forKey: "UserName") as! String)
                        self.Video_Image.append(tempitem.value(forKey: "VideoImage") as! String)
                    }
                    self.tableview.reloadData()
                }
            case .failure(let error):
                print(error)
            }

    }
}



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searching
    {
        return searchingvideos.count
    }
    else
    {
        return Video_Name.count
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell:VideoCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! VideoCell
    cell.V_Header.text = Video_Name[indexPath.row]
    cell.V_Footer.text = Video_User[indexPath.row]
    cell.V_Image.sd_setImage(with: URL(string: "\(AppDelegate.img_url)\(Video_User[indexPath.row]).jpg"), placeholderImage: #imageLiteral(resourceName: "v"))
    cell.U_Image.sd_setImage(with: URL(string: "\(AppDelegate.img_url)\(Video_Image[indexPath.row]).jpg"), placeholderImage: #imageLiteral(resourceName: "v"))



    if searching
    {
        cell.V_Header.text = searchingvideos[indexPath.row]
    }
    else
    {
        cell.V_Header.text  = Video_Name[indexPath.row]
    }
    return cell
}


func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
    searchingvideos = Video_Name.filter({$0.uppercased().prefix(searchText.count) == searchText.uppercased() })
    searching = true

    tableview.reloadData()
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchBar.showsCancelButton = true
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    searchBar.showsCancelButton = false
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
    searchBar.text = ""
}

0 个答案:

没有答案