27597

UITableView scroll to specific section using UIPIckerView?

I have a UITableView that has a fixed number of sections, however the number of rows in each section can vary depending on server results.

I would like to implement a picker wheel to "jump" to each section. Here are my UIPickerView delegate methods in the UITableViewController:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return 5; } -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ return [self.pickerArray objectAtIndex:row]; }

The "pickerArray" which is initialized in ViewDidLoad:

self.pickerArray = [[NSArray alloc]initWithObjects:@"Watching", @"Completed", @"On Hold", @"Dropped", @"Planned", nil];

and here's my didSelectRow method:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ [self.tableView scrollToRowAtIndexPath:[self.pickerArray objectAtIndex:row] atScrollPosition:UITableViewScrollPositionNone animated:YES]; }

I noticed there's no "scrollTo*section*AtIndexPath" method, which would be helpful. Apple's docs say this about the "indexpath" parameter:

indexPath An index path that identifies a row in the table view by its row index and its section index.

Calling the method (picking something in the picker) throws this fault:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString section]: unrecognized selector sent to instance 0x4bdb8'

Any idea what I should be doing?

Answer1:

The scrollToRowAtIndexPath method takes an NSIndexPath as the first parameter but the code is passing an NSString resulting in the exception.

As the docs say, an NSIndexPath includes both a section and row (you must know this since you populated a table view with sections).

You need to create an NSIndexPath that corresponds to the first row of the section in the table view that relates to the row selected in the picker view.

So assuming that the row of the picker view corresponds directly to the sections in your table view:

//"row" below is row selected in the picker view NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:row]; [self.tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionNone animated:YES];

Recommend

  • Multiple Picker Views on a single view
  • Custom object on NSPasteboard
  • Wrong area calculation objective-c?
  • UILabel created programatically - find it again?
  • How to prevent a “teardown” of my apk
  • Do Vue watched properties cache just like the computed properties?
  • How to determine when the text of an html element is changed
  • Compare two arrays and put equal objects into a new array [duplicate]
  • Compression in node.js
  • org.gnome.SessionManager in Ubuntu 12.04 chroot
  • Check if a UITableViewCell is completely visible
  • View Controller Loads Twice - How Do I Fix It?
  • UIBarButtonItem - Argument of '#selector' cannot refer to local function - Swift 3
  • updatesearchresultsforsearchcontroller not called
  • windowScriptObject method not found on Objective-C code
  • How to initialize global variable at each class invocation?
  • Printing input from TextField to a Label in Xcode with Swift
  • How do display a UIAlertView from a block on iOS?
  • How can integers (keys & values) be added and retrieved from a NSDictionary
  • Understanding Intl.DateTimeFormat as a JavaScript object
  • What do I do with this error when I run tests in rails?
  • “A GKScore must specify a leaderboard.”
  • MRO with multiple inheritance in python
  • NSMutableArray instance used in a block
  • Question about instantiating object
  • NSScanner Loop Question
  • gspread or such: help me get cell coordinates (not value)
  • Saving Changes After In-App Purchase Has Been Purchased
  • MongoDb aggregation
  • Row Count Is Returning the incorrect number using RaptureXML
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • Eraser for UIBezierPath
  • Display Images one by one with next and previous functionality
  • align graphs with different xlab
  • Unanticipated behavior
  • Compare two NSDates in iPhone
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Java static initializers and reflection
  • Can't mass-assign protected attributes when import data from csv file
  • Unable to use reactive element in my shiny app