UILocalizedIndexedCollat​​ion - [__ NSCFNumber length]:发送到实例的无法识别的选择器

时间:2014-01-03 06:01:19

标签: ios objective-c indexing

得到了这个错误-[__NSCFNumber length]: unrecognized selector sent to instance我做错了什么? songName是一个NSString,因此长度应该响应。

//  Song.h
@property (nonatomic, retain) NSNumber * pID;
@property (nonatomic, retain) NSString * serverID;
@property (nonatomic, retain) NSString * songName;
@property (nonatomic, retain) NSString * songURL;

// MAINVIEW

NSArray *indexedBooks = [self partitionObjects:self.songArray collationStringSelector:@selector(songName)];


-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
    self.collation = [UILocalizedIndexedCollation currentCollation];
    NSInteger sectionTitlesCount = [[self.collation sectionTitles] count];//section count is take from sectionTitles and not sectionIndexTitles
    NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionTitlesCount];

    NSLog(@"sectionTitleCount %i",sectionTitlesCount);

    //create an array to hold the data for each section
    for(int i = 0; i < sectionTitlesCount; i++)
    {
        [unsortedSections addObject:[NSMutableArray array]];
    }
    //put each object into a section
    for (id object in array)
    {
        NSInteger index = [self.collation sectionForObject:object collationStringSelector:selector];
        [[unsortedSections objectAtIndex:index] addObject:object];
    }

    NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionTitlesCount];

    //sort each section
    for (NSMutableArray *section in unsortedSections)
    {
        [sections addObject:[self.collation sortedArrayFromArray:section collationStringSelector:selector]];
    }

    NSLog(@"sections %@",sections); //error pointed to this line

    return sections;    

}

1 个答案:

答案 0 :(得分:0)

被证明是我的cellForRowAtIndexPath

的拼写错误
-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
    self.collation = [UILocalizedIndexedCollation currentCollation];
    NSInteger sectionCount = [[self.collation sectionTitles] count];//section count is take from sectionTitles and not sectionIndexTitles
    NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
    //create an array to hold the data for each section
    for(int i = 0; i < sectionCount; i++)
    {
        [unsortedSections addObject:[NSMutableArray array]];
    }
    //put each object into a section
    for (id object in array)
    {
        NSInteger index = [self.collation sectionForObject:object collationStringSelector:selector];
        [[unsortedSections objectAtIndex:index] addObject:object];
    }

    NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];

    //sort each section
    for (NSMutableArray *section in unsortedSections)
    {
        [sections addObject:[self.collation sortedArrayFromArray:section collationStringSelector:selector]];
    }
    return sections;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    SongCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.songName setFont:[UIFont fontWithName:monB size:17]];
    [cell.songArtistAlbum setFont:[UIFont fontWithName:monR size:10]];
    [cell.songArtistAlbum setTextColor:kCategoriesColor];

    if ( [[self.sectionsArray objectAtIndex:indexPath.section] count] != 0)
    {
        Song* songItem = [[self.sectionsArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        [cell.songName setTextColor:[self returnSongMoodColor:[songItem.mood intValue]]];
        NSString  *subText =[NSString stringWithFormat:@"%@  %@",[songItem.artistName length]>0 ? songItem.artistName : @"Unknown Artist", [songItem.albumTitle length]>0 ? songItem.albumTitle : @"Unknown Album"];
        NSMutableAttributedString *subTextAString = [[NSMutableAttributedString alloc]initWithString:subText];
        NSRange rangeAlbum = [subText rangeOfString:[songItem.albumTitle length]>0 ? songItem.albumTitle : @"Unknown Album"];
        [subTextAString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:rangeAlbum];
        [cell.songName setText:songItem.songName];
        [cell.songArtistAlbum setAttributedText:subTextAString];
        [cell.moodImg setImage:self.moodImg];

            return cell;
    }
    return nil;
}