Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7068859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:22:30+00:00 2026-05-28T05:22:30+00:00

I have a problem that has been stumping me for a while, I wrote

  • 0

I have a problem that has been stumping me for a while, I wrote a new version of the program and got the exact same bug.

When the following runs, I should be getting a nicely sectioned table view like,
1964
-Sam
-Ham
1965
-Nim Chimpsky
1980
-George
1985
-Bubbles

Instead I get
1964
-Sam
-Ham
1985
-Sam
1980
-Sam

Does anyone have any hints as to what I’m doing wrong?

My source code is below:

#import <UIKit/UIKit.h>


#pragma mark Monkey Object definition
@interface Monkey : NSObject {
    NSString *monkeyName; 
    NSString *monkeyBirthYear;
}

@property (copy) NSString *monkeyName; 
@property (copy) NSString *monkeyBirthYear; 

-(id)initWithMonkeyName:(NSString*)MN monkeyBirthYear:(NSString *)MBY; 


@end 

@implementation Monkey

@synthesize  monkeyName; 
@synthesize monkeyBirthYear; 

-(id)initWithMonkeyName:(NSString *)MN monkeyBirthYear:(NSString *)MBY; 
{
    if ((self =[super init])) {
        monkeyName = [MN copy]; 
        monkeyBirthYear = [MBY copy]; 

    }
    return self; 
}


@end

@interface ViewController : UITableViewController
{
    NSMutableArray *barrel; 
    NSMutableArray *monkeyBirthdayIndex; 

}
@end

@implementation ViewController





#pragma mark UITableView 
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
{
    return [monkeyBirthdayIndex objectAtIndex:section];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [monkeyBirthdayIndex count]; 
}

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    UITableViewCellStyle style = UITableViewCellStyleDefault; 
    UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    if(!cell) 
        cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:@"Cell"]; 

    Monkey *m = [barrel objectAtIndex:[indexPath row]]; 

    cell.textLabel.text = [m monkeyName]; 

    return cell; 
}


- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section 
{
    NSString *match = [monkeyBirthdayIndex objectAtIndex:section]; 

    NSArray *currentMonkey = [barrel filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.monkeyBirthYear LIKE[cd] %@", match]];
    return [currentMonkey count];

}



-(void)loadView 
{
    [super loadView]; 


    Monkey *monkey1 = [[Monkey alloc] initWithMonkeyName:@"Sam" monkeyBirthYear:@"1964"]; 
    Monkey *monkey2 = [[Monkey alloc] initWithMonkeyName:@"Ham" monkeyBirthYear:@"1964"]; 
    Monkey *monkey3 = [[Monkey alloc] initWithMonkeyName:@"Nim Chimpsky" monkeyBirthYear:@"1965"]; 
    Monkey *monkey4 = [[Monkey alloc] initWithMonkeyName:@"Bubbles" monkeyBirthYear:@"1985"]; 
    Monkey *monkey5 = [[Monkey alloc] initWithMonkeyName:@"George" monkeyBirthYear:@"1980"]; 

    barrel = [NSMutableArray arrayWithObjects:monkey1, monkey2, monkey3, monkey4,monkey5, nil]; 

    monkeyBirthdayIndex = [[NSMutableArray alloc] init]; 

    for (int i=0; i<[barrel count]; i++) {
        //Get the date of each monkeys birthday
        Monkey *m = [barrel objectAtIndex:i]; 

        if (![monkeyBirthdayIndex containsObject:[m monkeyBirthYear]])
        {
            [monkeyBirthdayIndex addObject:[m monkeyBirthYear]];
        }

    }



}

@end

@interface AppDelegate :NSObject <UIApplicationDelegate> 
{
    UIWindow *window; 
}
@end 

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    ViewController *vc = [[ViewController alloc] init]; 
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc]; 
    window.rootViewController = nav; 
    [window makeKeyAndVisible]; 
    return YES; 
}




int main(int argc, char *argv[])
{
    @autoreleasepool {
        int returnValue = UIApplicationMain(argc, argv, nil, @"AppDelegate");
        return returnValue; 
    }
}

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:22:31+00:00Added an answer on May 28, 2026 at 5:22 am

    In tableView:cellForRowAtIndexPath: you return the name of the nth monkey in the barrel:

    Monkey *m = [barrel objectAtIndex:[indexPath row]]; 
    

    What you really want is the nth monkey in a subarray filtered out as in tableView: numberOfRowsInSection:.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my problem. I have a program that has to run in a
I've got a small Cocoa problem. I have a StatusBar application that has an
I have a problem with CR (version that ships with VS2008). Report has two
I have a problem that has been bugging me all day. In my code
I have a problem that has been nagging me for some time now and
I have a problem that has been consuming me for days. I have a
This is a problem that has been eating at me for a while, and
Unfortunately I've got a bug that I have been working on for 1 full
I have a reoccurring DNS problem that has been plaguing our users, occasionally causing
I have a problem that has been effectively reduced to a Travelling Salesman Problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.