2016-10-19 82 views
1

我的問題如何在iOS中的UITableView處理動態部分和行

當我處理UITableView,我基本都與我部和行陣列table_data

[ 
    { 
     title : "section A title", 
     rows: [ 
      {data: row_1_data}, 
      {data: row_2_data} 
     ] 
    }, 
    { 
     title : "section B title", 
     rows: [ 
      {data: row_1_data}, 
      {data: row_2_data} 
     ] 
    }, 
] 

我用heightForHeaderInSectioncellForRowAtindexPathtitleForHeaderInSectiondidSelectRowAtindexPath方法,這樣

if indexPath.section == 0 { 
     //section A 
     if indexPath.row == 0 { 
      //do something with table_data[0]["rows"][0]["data"] 
     } 
     elesif indexPath.row == 1 { 
      //do something else 
     } 
} 
if indexPath.section == 1 { 
     //do something for section B 
} 

與數字01等工作變得很頭疼,當我table_data陣列是動態的。動態的意思是,某些部分或行可以有不同的位置或根本不存在。

例如,我刪除部分A和我的陣列是

[ 
    { 
     title : "section B title", 
     rows: [ 
      {data: row_1_data}, 
      {data: row_2_data} 
     ] 
    } 
] 

我喜歡這個

self.section_A_position = 0 
self.section_B_position = 1 
func afterRemoveSectionA() { 
    section_A_position = -999 
    section_B_position = section_B_position - 1 
    //write here another new sections for -1 
} 

添加另一個部分C作爲0元素

self.section_C_position = -999 
func afterAddSectionC() { 
    section_C_position = 0 
    section_A_position = section_A_position + 1 
    section_B_position = section_B_position + 1 
} 

然後用section_positions in函數

if indexPath.section == section_A_position { 
     //section A 
     if indexPath.row == 0 { 
      //do something with table_data[0]["rows"][0]["data"] 
     } 
     elesif indexPath.row == 1 { 
      //do something else 
     } 
} 
if indexPath.section == section_B_position { 
     //do something for section B 
} 

它看起來很簡單,但是當我有很多部分和很多情況下隱藏或移動它在數組中,很難控制和添加新類型的部分。有時我創建section_positions_map數組將其存儲在一起,並使+1,-1操作循環。但是當我需要這種行爲時,它並不能幫助它在每個TableViewController中組織它仍然很困難。

問題

你知道,使這部分更容易的任何方法或框架?

我的想法

  1. 添加type屬性我字典
{ 
    title : "section A title", 
    rows: [ 
     {data: row_1_data}, 
     {data: row_2_data} 
    ] 
    type : "section_a" 
} 

,並檢查if table_data[0]["type"] == "section_a"(或使用enum收集類型)

  • Subclass my Dictionar y和檢查
  • if table_data[0] is SubClassSectionA

    但他們兩人長相醜陋的我。

    回答

    2

    創建一個表,我知道所有的可能的部分當我使用的方法是用枚舉。你必須爲每個可能的部分類型創建一個枚舉:

    enum SectionTypes { case sectionA, sectionB, sectionC }

    ,然後創建一個變量來保存部分:

    var sections: [SectionTypes]

    當你有你的數據準備好就填充部分與需要顯示的部分。我平時也做的方法,以幫助部分:

    func getSection(forSection: Int) -> SectionTypes { 
         return sections[forSection] 
        } 
    

    有了這個地方,你可以開始實施通用的DataSource委託方法:

    func numberOfSections(in collectionView: UICollectionView) -> Int { 
        return sections.count 
    } 
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
        switch getSection(forSection: section) { 
        case .sectionA: 
         return 0 //Add the code to get the count of rows for this section 
        case .sectionB: 
         return 0 
        default: 
         return 0 
        } 
    } 
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
        switch getSection(forSection: indexPath.section) { 
        case .sectionA: 
         //Get section A cell type and format as your need 
        //etc 
        } 
    } 
    
    1

    使用NSDictionary存儲數據是一種不好的做法。爲部分(即SectionData)和行(即RowData)創建新的類。你的ViewController(或其他一些數據提供者)將保持SectionData的數組,其中SectionData保存RowData數組。 RowData可能包含用於處理行選擇FUNC,所以didSelectRowAtIndexPath方法你只是做這樣的事情:

    let rowData = rowDataAtIndexPath(indexPath) 
    rowData.tapHandler() 
    

    其中rowDataAtIndexPath是你定義的功能,讓您的數據indexPath。

    當你需要刪除某些部分時,只需從部分數組中刪除它並重新加載tableView即可。

    +0

    謝謝您的回答。這是有趣的方式。但是如果我在一節中有不同的rowData行爲呢? –

    +0

    對於每個行對象,函數rowData.tapHandler可以不同。你可以放任何你想要的東西。 –

    相關問題