gpt4 book ai didi

ios - UITableview 显示 NSMutableArray 中的重复对象

转载 作者:行者123 更新时间:2023-11-28 17:39:03 25 4
gpt4 key购买 nike

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

当下面的代码运行时,我应该得到一个很好的分区 TableView ,比如,1964年-山姆-火腿1965年-Nim Chimpsky1980-乔治1985-气泡

相反,我得到1964年-山姆-火腿1985-山姆1980-山姆

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

我的源代码如下:

#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

最佳答案

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

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

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

关于ios - UITableview 显示 NSMutableArray 中的重复对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8888768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com