简体   繁体   中英

Strange tableView row/section animation when row is selected

This cool expanding tableView works great but when you select some of the rows, a weird animation occurs.

The table has multiple sections, and each row can show additional rows when clicked. Row appearance and behavior is structured in a plist using various values eg if a row's boolean "isExpandable" property in the plist is set to true and its "additionalRows" property is set to 2, the row can expand to show 2 sub-rows.

The issue with the table is that when the first cell in each section is clicked, the animation runs fine, but when other cells are clicked, they shuffle:-

在此处输入图片说明

I suspect the strange animation happens because all sections are reloaded when a cell is clicked because of this line under didSelectRowAtIndexPath in the ViewController file:-

    tbl.reloadSections(NSIndexSet(index: indexPath.section), withRowAnimation: .Automatic)

I've tried the following but they either crash the app or cause the cells to not expand:-

  • Setting the animation method to .None
  • Using reloadData instead
  • Storing the indexPaths of the selectedRow and its sub-rows, and then reloading all with reloadIndexPaths
  • Storing the rows of the selected cell and its subcells then reloading them once when a row is clicked

Someone on the actual site that the code is posted on suggested using insertRowsAtIndexPath and deleteRowsAtIndexPath instead of reloadSection, but I'm not sure how to do it. Your help is greatly appreciated.

EDIT - Also tried insertRowsAtIndexPath and deleteRowsAtIndexPath but it seems to conflict with the way the tableView model is set up and the app crashes.

Inserting rows into the table should help you with more consistent table view animation behaviour.

To insert rows at specific index paths you can use the 'insertRowsAtIndexPaths' method. You will need to generate each index path that you are inserting and then pass them to the method in an array.

ObjC:

NSArray* arr = your NSIndex Paths...;

[tableView insertRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationFade];

Swift:

table.insertRowsAtIndexPaths([IndexPaths..], withRowAnimation: .Automatic)

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/occ/instm/UITableView/insertRowsAtIndexPaths:withRowAnimation :

Here is the problem: reloadSections(..) makes the tableView conveniently fetch all of the newest data from the model (for the specified section(s)) at the cost of more 'vague' animation. Use insertRowsAtIndexPaths(..) with the .Top animation style for the behavior you are looking for. If you have issues with your Data Model, fix that first (because it will almost certainly bother you later).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM