Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

webznz

macrumors member
Original poster
Mar 8, 2011
82
0
Hobbitin
Hey guys, I have been working on trying to make a Indexed UItableView for fast scrolling.. With lots of reading and help from different resources on the net I have everything (apart from one small error) set up.

I am creating the correct number of sections with the correct index to the right of the screen it is also scrollable however I have one error.. I cannot output the values in my NSDictionary without throwing an error.

Below I will show you where I am creating my dictionary then i will show you the tableview:cellForRowAtIndexPath method where I am trying to set the UITableViewCell TextLabel.. any help would be greatly appreciated.



Code:
//method to sort array and split for use with uitableview Index
- (IBAction)startSortingTheArray:(NSMutableArray *)arrayData
{
    //Sort incoming array alphabetically
    sortedArray = [arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    //NSLog(@"%@",sortedArray);
    
    // Dictionary will hold our sub-arrays
    self.arraysByLetter = [NSMutableDictionary dictionary];
    //arraysByLetter = [NSMutableDictionary dictionary];
    sectionLetters = [[NSMutableArray alloc] init];
    // Iterate over all the values in our sorted array
    for (NSString *value in sortedArray) {
        
        // Get the first letter and its associated array from the dictionary.
        // If the dictionary does not exist create one and associate it with the letter.
        NSString *firstLetter = [value substringWithRange:NSMakeRange(0, 1)];
        NSMutableArray *arrayForLetter = [arraysByLetter objectForKey:firstLetter];
        if (arrayForLetter == nil) {
            arrayForLetter = [NSMutableArray array];
            [arraysByLetter setObject:arrayForLetter forKey:firstLetter];
            [sectionLetters addObject:firstLetter]; // This will be used to set index and section titles
        }
        
        // Add the value to the array for this letter
        [arrayForLetter addObject:value];
    }
    // arraysByLetter will contain the result you expect
    NSLog(@"Dictionary: %@", arraysByLetter); //This prints what is currently in the NSDictionary
      
    //Reloads data in table
    [self.tableView reloadData];
}

With [sellf.tableView reloadData] it reload all the tableview delegates, then the method where I try to set the UITableViewCells up is called like so.

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    } 
    
    cell.accessoryType = UITableViewCellAccessoryNone; //make sure their are no tickes in the tableview 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; // no blue selection
    
    // Configure the cell...
    if(indexPath.row < [self.arraysByLetter count]){
        
        NSString *value = [self.arraysByLetter objectForKey:[self.sectionLetters objectAtIndex:indexPath.row]]; //<<error happen here.
        NSLog(@"thingy %@", value); //<<check below for output of this NSLog
    
        cell.textLabel.text = value;
   }
    return cell;
}

This is the output of the NSLog inside the if statment where I am tyring to set the textlabel.

Code:
2011-09-23 11:33:20.055 instaCode[9161:207] thingy (
    Honda,
    Honda,
    Honda,
    Honda,
    Honda,
    Honda,
    Honda
)


HOWEVER! if I comment out
Code:
//cell.textLabel.text = value;
This is the output I recive

Code:
2011-09-23 13:16:06.757 iCode[12438:207] thingy (
    Honda,
    Honda,
    Honda,
    Honda,
    Honda,
    Honda,
    Honda
)
2011-09-23 13:16:06.760 iCode[12438:207] thingy (
    Mazda,
    Mazda,
    Mitsubishi,
    Mitsubishi,
    Mitsubishi,
    Mitsubishi,
    Mitsubishi,
    Mitsubishi
)
2011-09-23 13:16:06.761 iCode[12438:207] thingy (
    Nissan,
    Nissan,
    Nissan,
    Nissan,
    Nissan,
    Nissan,
    Nissan
)
2011-09-23 13:16:06.762 iCode[12438:207] thingy (
    Toyota,
    Toyota,
    Toyota
)
2011-09-23 13:16:06.763 iCode[12438:207] thingy (
    Honda,
    Honda,
    Honda,
    Honda,
    Honda,
    Honda,
    Honda
)
2011-09-23 13:16:06.764 iCode[12438:207] thingy (
    Mazda,
    Mazda,
    Mitsubishi,
    Mitsubishi,
    Mitsubishi,
    Mitsubishi,
    Mitsubishi,
    Mitsubishi
)

Which is the correct output.. and dose not crash.. only problem being no values appear on the textlabel obviouly..

This is the error message I am getting when that line is not commented out.

Code:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM isEqualToString:]: unrecognized selector sent to instance 0x4e326c0'
*** Call stack at first throw:

I have pretty much tried everything I know and have read so any help would be HUGELY appreciated.
 
Last edited:
Objects in the arraysByLetter dictionary are arrays (as your name suggests), but you are assigning one to an NSString.
 
oh man.. So how would I pass each variabel of the arrays into a string for the UItableViewcell? To be honest this NSDictionary is the first I have tried to use and its got me quite confused.. I should have known that..
 
ahh.. almost cracked it.. however the each array in the NSDictionary is print to one uitableviewcell textlabel...

Code:
if(indexPath.row < [self.arraysByLetter count]){
        
        //NSArray *value = [self.arraysByLetter objectForKey:[self.sectionLetters objectAtIndex:indexPath.row]];
        NSArray *keys = [self.arraysByLetter objectForKey:[self.sectionLetters objectAtIndex:indexPath.row]];
        NSString *key = [keys description];
        NSLog(@"thingy %@", key);
        
        cell.textLabel.text = key;
   }
 
Okay finally got it with a little help from a friend of stack overflow..

I wasnt declaring the section correctly .. was trying to use the ifstatment todo that but thats wrong wrong wrong! my solution below.

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    } 
    
    cell.accessoryType = UITableViewCellAccessoryNone; //make sure their are no tickes in the tableview 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; // no blue selection
    
    // Configure the cell...
        
    NSArray *keys = [self.arraysByLetter objectForKey:[self.sectionLetters objectAtIndex:indexPath.section]];
    NSString *key = [keys objectAtIndex:indexPath.row];
    NSLog(@"thingy %@", key);
        
    cell.textLabel.text = key;
    
    return cell;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.