开发者

Can I drag sections in tableview as I can do it with rows?

I use

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath 
  toIndexPath:(NSIndexPath *)destinationIndexPath{
 }

method for dragging and dropping rows within and across sections. Can I perf开发者_如何学Pythonorm drags with sections as well?


It's probably conceptually (as well as technically) simpler to step back a level to a table view where each row corresponds to the sections of the table you want to re-order. Maybe you could invoke this with a "re-order sections" button, show the section-table-view, re-order, then impose that re-ordering on the detail table view. I think if you try to do it all inside a single table, you're going to frustrate yourself and your users. My 2 cents. -Mike


Old question, I know, but I just implemented something that does this myself so I wanted to share the answer in case it helps others who stumble across this.

My basic approach was to add a row representing the section to the top of each section. I also added a "Add a new section" row with editing style of UITableViewCellEditingStyleInsert at the end of each section to allow creation of new sections directly within the view. So, for each section the rows were like:

  • Row 0 = Section row (visually distinguished by setting different background color)
  • Rows 1 to x = normal data rows
  • Row x+1 = "Add a new section"

(I have a couple of simple methods to translate between "edit mode indexPath" and "real data indexPath".)

For moving sections, the proposed move is checked in the UITableViewDelegate Protocol method tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath: to ensure that the section row has moved "far enough" (above or below another section row). The returned indexPath is clipped to either just above or just below the nearest section row (depending on if moving up or down) to give the user a visual indication of where it will be moved.

In tableView:moveRowAtIndexPath:toIndexPath: the section change is made in the data source, then a reloadData message is sent to the tableView to display the change. reloadData is used instead of moveSection:toSection: because with the latter, the data source and display would be out of sync due to the row move and would cause an exception.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜