UITableview 显示的 NSMutableArray 中的重复对象
我有一个问题困扰了我一段时间,我编写了该程序的新版本并遇到了完全相同的错误。
当以下运行时,我应该得到一个很好的分区表视图,例如, 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
tableView:cellForRowAtIndexPath:
中,您返回桶中第 n 个猴子的名称:您真正想要的是过滤掉子数组中的第 n 个猴子,如
tableView: numberOfRowsInSection:
。In
tableView:cellForRowAtIndexPath:
you return the name of the nth monkey in the barrel:What you really want is the nth monkey in a subarray filtered out as in
tableView: numberOfRowsInSection:
.我更正后的 tableView:cellForRowAtIndexPath。
My corrected tableView:cellForRowAtIndexPath.