如何在tableView中使用索引搜索?

时间:2011-05-18 11:26:31

标签: iphone uitableview

在我的tableview中,名称和标题来自服务器,通过xml解析,如图所示。

in figure1 name and title are comes from server through xml parsing

在此tableview列表中,我想使用索引搜索,如下图所示。我怎么能这样做?

Image

2 个答案:

答案 0 :(得分:0)

您可以拥有字典数组,其中每个部分将由字典对象表示。字典的关键是标题部分,值将是部分中的行数组。

您可以使用NSPredicate从服务器搜索返回的数组并构造数据源数组。 NSPredicate Programming Guide

iphone & Objective C - Filtering an array using NSPredicate

答案 1 :(得分:0)

步骤-1:在故事板中创建UItableView。 -数据源,委托

步骤-2:导入-UITableViewDelegate,UITableViewDataSource

第3步:实施方法

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return self.arr.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCell
    cell.selectionStyle = .none
    let dictData = self.arr[indexPath.row] as? NSMutableDictionary
    let image = dictData?.object(forKey: "image") as! String
    cell.lblSchoolName?.text = dictData?.object(forKey: "name") as? String
    cell.lblSchoolAddress?.text = dictData?.object(forKey: "address") as? String
    cell.img?.sd_setImage(with: URL(string: image), placeholderImage: UIImage(named: "splash screen-test-compress.jpg"), options: [.continueInBackground,.lowPriority], completed: {(image,error,cacheType,url) in

        if error == nil
        {
            cell.img?.image = image
        }
        else
        {
            cell.img?.image =  UIImage(named: "splash screen-test-compress.jpg")
        }
    })
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    let dictData = self.arrAll[indexPath.row] as! NSMutableDictionary
    let AllEvent_VC = self.storyboard?.instantiateViewController(withIdentifier: "AllEvent_VC") as! AllEvent_VC
    let strSlug = String(dictData.object(forKey: "slug") as! String)
    AllEvent_VC.strSlug = strSlug
    self.navigationController?.pushViewController(AllEvent_VC, animated: true)
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

// self.TableViewHeight.constant = CGFloat(self.arrAllSchoolByDistData.count * 100)                                 //self.view.updateConstraintsIfNeeded()

相关问题