UIPickerView doesnt reloads until scrolling - objective-c

when I change my picker1, it was supposed to reload my picker2, but only occurs when I scrolls it. What can I do? Thanks!
#implementation ViewController
#synthesize picker1, array1, arrayApple, arraySamsung, picker2, array2;
- (void)viewDidLoad
{
[super viewDidLoad];
array1 = [[NSMutableArray alloc] initWithObjects:#"Apple", #"Samsung",nil];
arrayApple = [[NSMutableArray alloc] initWithObjects: #"iPhone 5", #"iPad", #"iPod Touch", #"MacBook Pro", nil];
arraySamsung = [[NSMutableArray alloc] initWithObjects: #"Galaxy Tab", #"Galaxy S3", #"Galaxy S", #"Galaxy 5", nil];
self.array2 = self.arrayApple;
[picker1 reloadAllComponents];
[picker2 reloadAllComponents];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
//Picker View Methods
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//NSLog(#"Selected Index: %#. Index of selected color: %i", [array1 objectAtIndex:row], row);
if(pickerView == self.picker1) {
if(row == 1) {
[self.array2 removeAllObjects];
self.array2 = self.arraySamsung;
[picker2 reloadAllComponents];
NSLog(#"%#", picker2);
}
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (pickerView == self.picker1) {
return [array1 count];
} else {
return [array2 count];
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (pickerView == self.picker1) {
return [self.array1 objectAtIndex:row];
} else {
return [self.array2 objectAtIndex:row];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

Try this
if(row == 1) {
self.array2 = self.arraySamsung;
} else {
self.array2 = self.arrayApple;
}
[picker2 reloadAllComponents];
NSLog(#"%#", picker2);

Related

uncaught exception 'NSRangeException'

I getting crash uncaught exception 'NSRangeException', reason: '*** __boundsFail: index 3 beyond bounds [0 .. 2]' in Objective C
crashlytics issues at this line "label.text = [_addressTypeArray objectAtIndex:row];"
please see the below code and help if possible
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
**- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (isStateButtonClicked == YES) {
return _stateTypeArray.count;
} else
return _addressTypeArray.count;
}
#pragma mark - UIPickerViewDelegate
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc]init];
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont fontWithName:kGothamMediumFontName size:17.0];
if (isStateButtonClicked == YES) {
NSString *stateName = [NSString stringWithFormat:#"%# - %#", [[_stateTypeArray objectAtIndex:row] objectForKey:#"StateName"], [[_stateTypeArray objectAtIndex:row] objectForKey:#"StateFullName"]];
label.text = stateName;
}
else {
label.text = [_addressTypeArray objectAtIndex:row];
}
return label;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView == self.addressTypePickerView)
{
self.addressTypePickerView.hidden = NO;
orderPickerContainerView.hidden = NO;
disableView.hidden = NO;
if (isStateButtonClicked == YES) {
self.stateRow = row;
self.stateTypeSelected = [[_stateTypeArray objectAtIndex:row] objectForKey:#"StateName"];
} else {
self.addressTypeRow = row;
self.addressTypeSelected = [_addressTypeArray objectAtIndex:row];
}
}
if (pickerView == self.stateTypePickerView)
{
self.stateRow = row;
self.stateTypeSelected = [[_stateTypeArray objectAtIndex:row] objectForKey:#"StateName"];
}
[self.addressTypePickerView reloadAllComponents];
self.scrollViewMain.contentSize = CGSizeMake(self.bounds.size.width, 550.0);
}
The issue is your button state. It was changed without telling the picker that it has to reload its data.
Have a look into the code you call for the button. There should be a line calling
reloadallcomponents on the picker.

NSRangeException when I have multiple pickerViews for multiple textfields

#import "ProfileViewController.h"
#implementation ProfileViewController{
NSArray *currentArray;
UITextField *currentTextField;
}
#synthesize picker, Feets, Inchs, Weights, Months, Days, Years, HeightValue, WeightValue, DOBValue;
#synthesize scrollView;
int variabla;
// Control the textfield go up when tap the textfield and keyboard coming out
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGPoint scrollPoint = CGPointMake(0, textField.frame.origin.y);
[scrollView setContentOffset:scrollPoint animated:YES];
[textField resignFirstResponder];
[picker setHidden:YES];
currentTextField = textField;
NSLog(#"222222");
if (currentTextField == HeightValue)
{
NSLog(#"3333333");
[HeightValue resignFirstResponder];
[picker setHidden:NO];
variabla = 1;
}
else if (currentTextField == WeightValue)
{
NSLog(#"4444444");
[WeightValue resignFirstResponder];
[picker setHidden:NO];
variabla = 2;
}
else if (currentTextField == DOBValue){
NSLog(#"5555555");
[DOBValue resignFirstResponder];
[picker setHidden:NO];
variabla = 3;
}
NSLog(#"variabla %d",variabla);
[picker reloadAllComponents];
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"111111");
NSString *path = [[NSBundle mainBundle]pathForResource:#"WeightList" ofType:#"plist"];
Weights = [[NSMutableArray alloc]initWithContentsOfFile:path];
[picker setHidden:YES];
Feets = [[NSMutableArray alloc]initWithObjects:#"0ft", #"1ft", #"2ft", #"3ft", #"4ft", #"5ft", #"6ft", #"7ft", #"8ft", #"9ft",nil];
Inchs = [[NSMutableArray alloc]initWithObjects:#"0in", #"1in", #"2in", #"3in", #"4in", #"5in", #"6in", #"7in", #"8in", #"9in", #"10in", #"11in",nil];
Months = [[NSMutableArray alloc]initWithObjects:#"1", #"2", #"3",nil];
Days = [[NSMutableArray alloc]initWithObjects:#"1", #"2", #"3",nil];
Years = [[NSMutableArray alloc]initWithObjects:#"1", #"2", #"3",nil];
#pragma mark - UIPcikerView DataSource and Delegate method
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
if (variabla == 1){
return 2;
}
else if (variabla == 2){
return 1;
}
else
return 3;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (variabla == 1){
if (component == feetComponent)
return [Feets count];
if (component == inchComponent)
return [Inchs count];
}
if (variabla == 2){
return [Weights count];
}
else{
if (component == monthComponent)
return [Months count];
if (component == dayComponent)
return [Days count];
else
return [Years count];
}
}
// set the row text to the textfield
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED{
if (variabla == 1){
if (component == feetComponent)
return Feets[row];
if (component == inchComponent)
return Inchs[row];
}
if (variabla == 2){
return Weights[row];
}
else{
if (component == monthComponent)
return Months[row];
if (component == dayComponent)
return Days[row];
else
return Years[row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED{
if (currentTextField == HeightValue){
NSInteger feetRow = [picker selectedRowInComponent:feetComponent];
NSInteger inchRow = [picker selectedRowInComponent:inchComponent];
NSString *feet = Feets[feetRow];
NSString *inch = Inchs[inchRow];
NSString *msg = [[NSString alloc]initWithFormat:#"%# %#", feet, inch];
HeightValue.text = msg;
}
if (currentTextField == WeightValue){
NSInteger weightRow = [picker selectedRowInComponent:weightComponent];
NSString *weight = Weights[weightRow];
NSString *msg2 = [[NSString alloc]initWithFormat:#"%#",weight];
WeightValue.text = msg2;
}
if (currentTextField == DOBValue){
NSInteger monthRow = [picker selectedRowInComponent:monthComponent];
NSInteger dayRow = [picker selectedRowInComponent:dayComponent];
NSInteger yearRow = [picker selectedRowInComponent:yearComponent];
NSString *month = Months[monthRow];
NSString *day = Days[dayRow];
NSString *year = Years[yearRow];
NSString *msg3 = [[NSString alloc]initWithFormat:#"%# / %# / %#", month, day, year];
WeightValue.text = msg3;
}
}
#end
Above is the .m file code. When I run the simulator, the first textfield for the Height is working fine, but in the Weight and Date textfield, the picker view got the exception.
Simulator screen shot
NSRangeException screen shot
Well the exception says Index 5 beyond bounds [0..0]. So that says you have an array of size 1 but are asking it for the object at index 5, which doesn't make sense, so it crashes.
The traceback says that objectAtIndex method is being called from selectedRowInComponent:, which is being called from your didSelectRow:inComponent. So that suggests that you're passing an invalid component number to selectedRowInComponent.
I don't know where weightComponent and month/day/yearComponent are being set, but I'm guessing that they are not being set to 0 and 0, 1, and 2 respectively.
As an aside, I would also add that when you have a picker with labels 0ft, 1ft etc, that you don't keep around all these tables. Instead of creating a table and indexing it, just answer the titleForRow call with return [NSString stringWithFormat:#"%dft",row];

2 UI picker views in one view interfering with each UI Picker's text view

I'm building an app with two UI textFields. When the UI textfields are touched a UI Picker comes up for the user to select a value to stay in the UI textField. When I test the app and touch one UI TextView and scroll the UI Picker, both UI textFields change values. Meaning, each UI Text Field is being affected from each UI Picker. Anyone know why? Thank you.
#interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
#end
#implementation ViewController
#synthesize ageTextField;
#synthesize relationshipTextField;
#synthesize resultLabel;
#synthesize calculateButton;
#synthesize agePickerView;
#synthesize relationshipPickerView;
#synthesize ageArray;
#synthesize relationshipArray;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//age pickerView
self.agePickerView = [[UIPickerView alloc]init];
self.agePickerView.delegate = self;
self.agePickerView.dataSource = self;
self.agePickerView.showsSelectionIndicator = YES;
self.ageTextField.inputView = self.agePickerView;
self.ageArray = #[ #"1", #"2", #"3", #"4", #"5", #"5", #"6", #"7", #"8", #"9", #"10", #"11", #"12"
, #"13", #"14", #"15", #"16", #"17", #"18", #"19", #"20", #"21", #"22", #"23", #"24", #"25", #"26", #"27", #"28", #"29", #"30", #"31", #"32", #"33", #"34", #"35", #"36", #"37", #"38", #"39", #"40", #"41", #"42", #"43", #"44", #"45", #"46", #"47", #"48", #"49", #"50", #"51", #"52", #"53", #"54", #"55", #"56", #"57", #"58", #"59", #"60", #"61", #"62", #"63", #"64", #"65", #"66", #"67", #"68", #"69", #"70", #"71", #"72", #"73", #"74", #"75", #"76", #"77", #"78", #"79", #"80", #"81", #"82", #"83", #"84", #"85", #"86", #"87", #"88", #"89", #"90", #"91", #"92", #"93", #"94", #"95", #"96", #"97", #"98", #"99", #"100" ];
[self pickerView:self.agePickerView
didSelectRow:0
inComponent:0];
//relationship pickerView
self.relationshipPickerView = [[UIPickerView alloc]init];
self.relationshipPickerView.delegate = self;
self.relationshipPickerView.dataSource = self;
self.relationshipPickerView.showsSelectionIndicator = YES;
self.relationshipTextField.inputView = self.relationshipPickerView;
self.relationshipArray = #[ #"1", #"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13", #"14",#"15",#"16",#"17",#"18",#"19",#"20",#"21",#"22",#"23",#"24",#"25", #"26",#"27",#"28",#"29",#"30",#"31",#"32",#"33",#"34",#"35",#"36",#"37", #"38",#"39",#"40",#"41",#"42",#"43",#"44",#"45",#"46",#"47",#"48",#"49",#"50", #"51", #"52", #"53", #"54", #"55", #"56", #"57", #"58", #"59", #"60", #"61", #"62", #"63", #"64", #"65", #"66", #"67", #"68", #"69", #"70", #"71", #"72", #"73", #"74", #"75", #"76", #"77", #"78", #"79", #"80", #"81", #"82", #"83", #"84", #"85", #"86", #"87", #"88", #"89", #"90", #"91", #"92", #"93", #"94", #"95", #"96", #"97", #"98", #"99", #"100"];
[self pickerView:self.relationshipPickerView
didSelectRow:0
inComponent:0];
}
-(int) numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(int) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.ageArray count];
return [self.relationshipArray count];
}
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [self.ageArray objectAtIndex:row];
return [self.relationshipArray objectAtIndex:row];
}
-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component{
self.ageTextField.text = [self.ageArray objectAtIndex:row];
self.relationshipTextField.text = [self.relationshipArray objectAtIndex:row];
}
- (void)viewDidUnload
{
[self setAgeTextField:nil];
[self setRelationshipTextField:nil];
[self setResultLabel:nil];
[self setCalculateButton:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
-(int) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.ageArray count];
return [self.relationshipArray count];
}
That's not how C (or Objective-C) works. It will always only execute the first line.
You need to conditionalize the return value based on which UIPickerView is asking for the information:
if (pickerView == self.agePickerView) {
return [self.ageArray count];
} else {
return [self.relationshipArray count];
}
And then apply that same pattern to all the datasource methods.

Invalid argument type 'NSString' to unary expression

I am having trouble with this method which returns an error (commented out) even though I copied it from the Apple documentation.
Can you tell me what's wrong?
Thanks in advance.
#import "BIDDependentComponentViewController.h"
#implementation BIDDependentComponentViewController
- (IBAction)buttonPressed
{
NSInteger stateRow = [self.dependentPicker selectedRowInComponent:kStateComponent];
NSInteger zipRow = [self.dependentPicker selectedRowInComponent:kZipComponent];
NSString *state = self.states[stateRow];
NSString *zip = self.zips[zipRow];
NSString *title = [[NSString alloc] initWithFormat:#"You selected zip code %#", zip];
NSString *message = [[NSString alloc] initWithFormat:#"%# is in %#", zip, state];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSBundle *bundle = [NSBundle mainBundle];
NSURL *plistURL = [bundle URLForResource:#"statedictionary" withExtension:#"plist"];
self.stateZips = [NSDictionary dictionaryWithContentsOfURL:plistURL];
NSArray *allStates = [self.stateZips allKeys];
NSArray *sortedStates = [allStates sortedArrayUsingSelector:#selector(compare:)];
self.states = sortedStates;
NSString *selectedState = self.states[0];
self.zips = self.stateZips[selectedState];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark-
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == kStateComponent)
{
return[self.states count];
} else {
return[self.zips count];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(component == kStateComponent)
{
NSString *selectedState = self.states[row];
self.zips = self.stateZips[selectedState];
[self.dependentPicker reloadComponent:kZipComponent];
[self.dependentPicker selectRow:0 inComponent:kZipComponent animated:YES];
}
#pragma mark Picker Delegate Methods
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component //Invalid argument type 'NSString' to unary expression
{
if(component == kStateComponent)
{
return self.states[row];
} else {
return self.states[row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(component == kStateComponent)
{NSString *selectedState = self.states[row];
self.zips = self.stateZips[selectedState];
[self dependentPicker selectRow:0
inComponent:kZipComponent
animated:YES];
}
#end // Missing #end
You're missing the closing brace } on this method:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
Putting that brace in the correct spot (just above #pragma mark Picker Delegate Methods
) will fix both of your errors.

Invalid argument type to unary expression

I have this program with a tableView as my first view. I have also implemented (or at least tried to) a search bar on top of the view. Have used several hours to search for a solution, but without positive results.
#import "FirstViewController.h"
#import "NSDictionary-MutableDeepCopy.h"
#implementation FirstViewController
#synthesize listData, table, search, allNames, names, keys;
#pragma mark -
#pragma mark Custom Methods
- (void)resetSearch {
NSMutableDictionary *allNamesCopy = [self.allNames mutableDeepCopy];
self.names = allNamesCopy;
[allNamesCopy release];
NSMutableArray *keyArray = [[NSMutableArray alloc] init];
[keyArray addObjectsFromArray:[[self.allNames allKeys]
sortedArrayUsingSelector:#selector(compare:)]];
self.keys = keyArray;
[keyArray release];
}
-(void)handleSearchForTerm:(NSString *)searchTerm {
NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
[self resetSearch];
for (NSString *key in self.keys) {
NSMutableArray *array = [names valueForKey:key];
NSMutableArray *toRemove = [[NSMutableArray alloc] init];
for (NSString *name in listData) {
if ([name rangeOfString:searchTerm
options:NSCaseInsensitiveSearch].location == NSNotFound)
[toRemove addObject:name];
}
if ([array count] == [toRemove count])
[sectionsToRemove addObject:key];
[array removeObjectsInArray:toRemove];
[toRemove release];
}
[self.keys removeObjectsInArray:sectionsToRemove];
[sectionsToRemove release];
[table reloadData];
}
- (void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:#"sortednames" ofType:#"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.names = dict;
self.allNames = dict;
[dict release];
[self resetSearch];
[table reloadData];
[table setContentOffset:CGPointMake(0.0, 44.0)animated:NO];
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
NSArray *array = [[NSArray alloc] initWithObjects:
// A larger amount of objects here.
self.listData = array;
[array release];
[super viewDidLoad];
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.listData = nil;
self.table = nil;
self.search = nil;
self.allNames = nil;
self.names = nil;
self.keys = nil;
}
- (void)dealloc {
[listData release];
[search release];
[table release];
[allNames release];
[keys release];
[names release];
[super dealloc];
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ([keys count] > 0) ? [keys count] : 1;
}
- (NSInteger)tableView:(UITableView *)aTableView
numberOfRowsInSection: (NSInteger)section {
return [self.listData count];
if ([keys count] == 0)
return 0;
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
return [nameSection count];
}
- (UITableViewCell *) extracted_method: (UITableViewCell *) cell {
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
static NSString *sectionsTableIdentifier = #"sectionsTableIdentifier";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:
sectionsTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: sectionsTableIdentifier] autorelease];
}
cell.backgroundColor = [UIColor clearColor];
cell.textColor = [UIColor whiteColor];
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.text = [nameSection objectAtIndex:row];
[self extracted_method: cell].text = [listData objectAtIndex:row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
if ([keys count] == 0)
return nil;
NSString *key = [keys objectAtIndex:section];
return key;
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return keys;
}
#pragma mark -
#pragma mark Table View Delegate Methods
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[search resignFirstResponder];
return indexPath;
}
#pragma mark -
#pragma mark Search Bar Delegate Methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSString *searchTerm = [searchBar text];
[self handleSearchForTerm:searchTerm];
}
- (void)searchBar:(UISearchBar *)searchBar
textDidChange:(NSString *)searchTerm {
if ([searchTerm length] == 0) {
[self resetSearch];
[table reloadData];
return;
}
[self handleSearchForTerm:searchTerm];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
search.text = #"";
[self resetSearch];
[table reloadData];
[searchBar resignFirstResponder];
}
#end
Ok guys. My problem is that this doesnt get the search function to work. In addition I receive siginal SIGABRT at this line:
NSString *key = [keys objectAtIndex:section];
So I need help with two things:
1: I need to get that SIGABRT away.
Error log message: * Terminating app due to uncaught exception
'NSRangeException', reason: '* -[NSMutableArray objectAtIndex:]:
index 0 beyond bounds for empty array'
That is I don't store any data in keys. how would I do that? and what would I store?
2: Want the search function to search in my listData array!
Thanks in advance - hope u can help!
You have not finished your sectionIndexTitlesForTableView: method. Right now it is:
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return keys;
There is no closing }, so the compiler thinks everything after that is still part of that method. When you try to define the next method, use use - (NSIndexPath *) to indicate that it is an instance method which returns NSIndexPath*, but the compiler thinks you are trying to subtract something.
The solution is simple: Add the } to the end of sectionIndexTitlesForTableView:.
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return keys;
}