Monday 15 February 2010

iphone - How to expand and collapse rows using header section in a UITableView -


I have a table view. Now I want to grind and expand the rows by tapping on the section headers. In other words, when I display the rows for that section in the header, how can I do this?

Let me prepare some code to give you this idea. The concept is that we can keep track of the collapsed section in NSMutableSet and add / remove it according to the user's touch on the section. Fall / Expansion animation is actually animation to add / remove cells.

  #import "ViewController.h" @ Interface View Controller () & lt; UITableViewDataSource, UITableViewDelegate & gt; {NSMutableSet * _collapsedSections; } @ Property (Nonomatic, Weak) IBOutlet UITWview * Tableview; @end @implementation ViewController - (zero) viewDidoadload {[Super Viewedload]; _collapsedSections = [NSMutableSet new]; } - (NSInteger) numberOfSectionsInTableView: (UITableView *) tableview {return3; } - (NSInteger) Table view: (UITableView *) table view numberofrease ins: (NSInteger) section {return [_collapsedSections are included in the object: @ (section)]? 0: 10; } - (NSArray *) indexPathsForSection: (intestine) section withNumberOfRows: (int) numberOfRows {NSMutableArray * indexPaths = [NSMutableArray new]; For (Int i = 0; I & lt; Numberofro; I ++) {NSINXPath * IndexPath = [NSINXXPath IndexPathfire: I In Size: section]; [Add index object: indexpath]; } Return Index Path; } - (zero) section button touchup inside: (UIButton *) Sender {sender.backgroundColor = [UIColor greenColor]; [Self.tableView startupdates]; Int section = Sender Tag; Bool shouldCollapse =! [_ CollapsedSections includes the object: @ (section)]; If bracket (required) {insunsofoorage = [self-telescopic view number inoperation: section]; NSArray * indexPaths = [Self index paraphrase: number section with numerator: numOfRows]; [Self.tableView deleteRowsAtIndexPaths: Rune animation with indexpath: UITableViewRowAnimationTop]; [_collapsedSections addObject: @ (section)]; } Else {int numOfRows = 10; NSArray * indexPaths = [self coordinate pageforrestation: section withNumberOfRows: numOfRows]; [Self.tableView insertRowsAtIndexPaths: Index Pathway with Animation: UITableViewRowAnimationTop]; [Delete _collapsedSectionsObject: @ (section)]; } [Self.tableView and Updates]; // [_ tableView reload data]; } - (UIView *) Talikadrishy: (UITableView *) Table View ForHeaderInSection: (NSInteger) section {UIButton * result = [UIButton button WithType: UIButtonTypeCustom]; [Result addTarget: Self-action: @Selector (sectionbutton to upide inside :) control events: UIControlEventTouchUpInside]; result. Background color = [UIColor blueColor]; [Result setTitle: [NSString stringWithFormat: @ "stream% d", section] forState: UIControlStateNormal]; Result.tag = section; Return result; } - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {UITableViewCell * result = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @ "cell"]; Result.textLabel.text = [NSString stringWithFormat: @ "cell% d", indexPath.row]; Return result; } @end    

No comments:

Post a Comment