2016-11-22 96 views
0

所以我有一個結構數組,我從我的json添加數據。同一陣列中有不同類型的數據。只有在滿足某些條件時才添加單元格

struct PersonData { 
    let name: String 
    let age: String 
    let sex : String 
} 

我想要做的是實現一個pickerView,它將根據用戶選擇的內容重新加載表視圖。可以說我有2個選擇器視圖,用戶可以選擇性別和年齡。

因此,如果他選擇了所有17歲的男性,我只想在桌面視圖上顯示。

我已經可以得到陣列上的計數,但我不能在UITableViewCell方法

我想要做這樣的事情返回nil:

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

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

     if(option == 0) //All persons. Default option 
     { 
      cell.name.text = dataArray[indexPath.row].name 
      return cell 

     } 
     else 
     { 
      if(dataArray[indexPath.row].age == agePickerOption || dataArray[indexPath.row].sex == sexPickerOption) 
      { 
       cell.name.text = dataArray[indexPath.row].name 
       return cell 
      } 
     } 

     return nil 

    } 

我知道我不能回零,因爲他期望一個UITableViewCell,但是如果滿足某些條件,它可能只增加indexPath?

或者我需要添加單元格並將其刪除後?這聽起來不對。

回答

1

我將有兩個數據數組:

allDataArray - 所有元素

filteredDataArray - 元素也應該符合您的過濾器

如果使用filteredArray作爲一個DataSource你不必將該邏輯放入cellForRow方法中。

相反,使用機械臂委託方法來過濾allDataArray,並把它放在filteredDataArray

+0

該死的我怎麼錯過了?這種簡單的解決方案。我不知道我怎麼會錯過使用一個數組作爲數據源。非常感謝Lopes! – Adrian

+1

沒問題;)良好的編碼 – LopesFigueiredo

相關問題