ios - why it needs tableView(_:indentationLevelForRowAt:) when Inserting cell into a table view with static cells -
background
the story come second example of book ios apprentice (ed. 6 2016)
a uitableview 2 section created. in storyboard following content had been designed: section 0 has 1 row fulfilled 1 static cell, section 1 has 2 row full filled 2 static cell.
what achieve
when tap last row of section 1 (ie, duedate row in picture a) new cell uidatepicker inserted tableview (please see picture b)
how author solve problem
a uitableviewcell filled uidatepicker added in scene dock in storyboard (please see picture c), new tablecell added table when duedate row tapped
table view static cells not have data source , therefore not use function “cellforrowat”. in order insert cell table following data source functions had been overridden
tableview(_:numberofrowsinsection:) tableview(_:cellforrowat:) tableview(_:heightforrowat:) tableview(_:indentationlevelforrowat:) problem
the function tableview(_:indentationlevelforrowat:) confuse me lot! , here full code
override func tableview(_ tableview: uitableview, indentationlevelforrowat indexpath: indexpath) -> int { var newindexpath = indexpath if indexpath.section == 1 && indexpath.row == 2 { newindexpath = indexpath(row: 0, section: indexpath.section) } return super.tableview(tableview, indentationlevelforrowat: newindexpath) } question
what meaning of function? what's purpose of it? i've read document, didn't information.
when third row/datepicker cell inserted (indexpath.section == 1 && indexpath.row == 2) why function indent first row newindexpath = indexpath(row: 0, section: indexpath.section) ?
implementing tableview(_:indentationlevelforrowat:) allows display table rows in hierarchal / tree-view type format. in:
level 1 sub-level 1a sub-level 1b level 2 sub-level 2a sub-sub-level 2aa sub-sub-level 2ab etc... commonly, 1 might use:
override func tableview(_ tableview: uitableview, indentationlevelforrowat indexpath: indexpath) -> int { if let myobj = mydata[indexpath.row] as? myobject { return myobj.myindentlevel } return 0 } for example present, without reading book...
it appear author decided new row (2) contains uidatepicker should have same indent level first row (0) in section. if it's not row 2, return default / current indentation level.
although, intent of
var newindexpath = indexpath if indexpath.section == 1 && indexpath.row == 2 { newindexpath = indexpath(row: 0, section: indexpath.section) } return super.tableview(tableview, indentationlevelforrowat: newindexpath) might seem little more obvious / clear with
newindexpath = indexpath(row: 0, section: 1) since limiting section == 1.

Comments
Post a Comment