如何在tableview中出列正确的自定义单元格类型:cellForRow:AtIndexPath

时间:2016-08-18 12:34:08

标签: ios swift uitableview

我正在做一个SearchBar,当你输入单词“nike”时,它必须显示自定义单元格“商店类型”,其标题中包含该名称,但如果它存在于品牌数组中,则必须显示品牌名称。

目前我设法在表格视图中获得正确的行数:

@Test
    public void XMLTest() throws Exception {

        final CountDownLatch unitTestLatch = new CountDownLatch(1);
        String url = "http://www.cbr.ru/scripts/XML_dynamic.asp?VAL_NM_RQ=R01235&date_req1=01.08.2016&date_req2=18.08.2016&rt=1&mode=1[1]";

        Context appContext = InstrumentationRegistry.getTargetContext();
        RequestQueue queue = Volley.newRequestQueue(appContext);

        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
              new Response.Listener<String>() {
                  @Override
                  public void onResponse(String response) {

                      try {
                          Serializer serializer = new Persister();
                          ValCurs valcur = serializer.read(ValCurs.class, new StringReader(response), false);
                          Log.d("TAG", "Record size: " + valcur.records.size());

                      } catch (Exception e) {
                          e.printStackTrace();
                      }

                      unitTestLatch.countDown();
                  }
              }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                unitTestLatch.countDown();
            }
        });

        queue.add(stringRequest);

         unitTestLatch.await();
    }

还设法过滤品牌和商店阵列:

var driver = require('webdriverio');
var client = driver.remote({
    desiredCapabilities: {
        browserName: 'chrome',
        chromeOptions: {
            args: [
                'disable-extensions'
            ]
        }
    }
});

但现在我陷入func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if(searchActive) { return filtered.count + filteredBrands.count } return shops.count; }

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {

    filtered = shopNames.filter({ (text) -> Bool in
        let tmp: NSString = text
        let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
        return range.location != NSNotFound
    })

    filteredBrands = brandNames.filter({ (text) -> Bool in
        let tmp: NSString = text
        let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
        return range.location != NSNotFound
    })

    if(filtered.count == 0 && filteredBrands.count == 0){
        searchActive = false
    } else {
        searchActive = true
    }

    //user pressed the x button to clean content
    if(searchText == "") {
        searchActive = false
    }

    currentTableView.reloadData()
}

我真的不知道如何将正确的细胞出列,并且在重复使用细胞时我遇到了奇怪的错误。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我认为这个例子可以帮助你

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }

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

    if(section == 0)
       {
         return filtered.count
       }

      return filteredBrands.count
   }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
                   var tableCell2 : UITableViewCell
    if(indexPath.section == 0)
    {
        tableCell2 = tableView.dequeueReusableCellWithIdentifier("ShopCell") as! ShopCell
    }else
    {
        tableCell2 = tableView.dequeueReusableCellWithIdentifier("BrandsCell") as! BrandsCell
    }

      return tableCell2
   }

我希望这有助于你