我有一个困扰我一段时间的问题,我写了一个新版本的程序并得到了完全相同的错误。
当以下运行时,我应该得到一个很好的分段表视图,如, 1964年 萨姆 -火腿 1965年 -Nim Chimpsky 1980年 -乔治 1985年 -Bubbles
相反,我得到了 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
答案 0 :(得分:0)
在tableView:cellForRowAtIndexPath:
中,你会在桶中返回第n只猴子的名字:
Monkey *m = [barrel objectAtIndex:[indexPath row]];
你真正想要的是在tableView: numberOfRowsInSection:
中过滤出的子阵列中的第n只猴子。
答案 1 :(得分:0)
我更正的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;
}