我有一个问题,一直困扰着我一段时间,我写了一个新版本的程序,并得到了完全相同的错误。UITableview显示NSMutableArray中的重复对象
当出现以下运行时,我应该得到像一个很好的分段表视图, 1964年 萨姆 -Ham 1965年 -Nim Chimpsky 1980年 - 乔治 -Bubbles
相反,我获得1964年 萨姆 -Ham 萨姆 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
Nim Chimpsky。我笑了。 –