How to deal with dynamic Sections and Rows in iOS UITableView

后端 未结 3 1641
深忆病人
深忆病人 2020-12-10 21:07

My issue

When I deal with UITableView, I basically have an Array table_data with my sections and rows.



        
3条回答
  •  一整个雨季
    2020-12-10 21:41

    It is a bad practice to use NSDictionary for storing data. Create new class for section (i.e. SectionData) and row (i.e. RowData). Your ViewController (or some other data provider) will keep array of SectionData, where SectionData keeps array of RowData. RowData may contain func for handling row selection, so in didSelectRowAtindexPath you just do something like this:

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

    where rowDataAtIndexPath is your defined function that gives you data for indexPath.

    When you need to remove some section, just remove it from array of sections and reload tableView.

提交回复
热议问题