UITableview 显示的 NSMutableArray 中的重复对象

发布于 2024-12-27 14:43:27 字数 3900 浏览 0 评论 0原文

我有一个问题困扰了我一段时间,我编写了该程序的新版本并遇到了完全相同的错误。

当以下运行时,我应该得到一个很好的分区表视图,例如, 1964年 -萨姆 -火腿 1965年 ——尼姆·钦普斯基 1980年 -乔治 1985年 -气泡

相反我得到 1964年 -萨姆 -火腿 1985年 -萨姆 1980年 -Sam

有人对我做错了什么有任何提示吗?

我的源代码如下:

#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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独自唱情﹋歌 2025-01-03 14:43:27

tableView:cellForRowAtIndexPath: 中,您返回桶中第 n 个猴子的名称:

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

您真正想要的是过滤掉子数组中的第 n 个猴子,如 tableView: numberOfRowsInSection:

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:.

李不 2025-01-03 14:43:27

我更正后的 tableView:cellForRowAtIndexPath。

   - (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     
    NSInteger section = [indexPath section];     
    UITableViewCellStyle style = UITableViewCellStyleDefault; 

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

    NSString *match = [monkeyBirthdayIndex objectAtIndex:section]; 

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

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

    cell.textLabel.text = m.monkeyName;

    return cell; 
}

My corrected tableView:cellForRowAtIndexPath.

   - (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     
    NSInteger section = [indexPath section];     
    UITableViewCellStyle style = UITableViewCellStyleDefault; 

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

    NSString *match = [monkeyBirthdayIndex objectAtIndex:section]; 

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

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

    cell.textLabel.text = m.monkeyName;

    return cell; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文