【发布时间】:2010-11-20 07:27:09
【问题描述】:
所以,我有一个核心数据对象,我们称它为会话(好吧,这就是它的实际名称),它有四个属性(名称、驾驶员、轨道和汽车),我想在其中显示表格视图。我以前用过它,但是,唉,我正试图让我的视图控制器更加通用和可重用,所以,我正在对其进行一些更改。任何人,这就是桌子的样子......
传入视图控制器的是一个 Session,它是 CoreData 为我准备的 NSManagedObject 的子类。 Driver、Car 和 Track 都是对象关系,而 name 只是一个字符串。 Driver、Car 和 Track 都有我在此表中显示的名称属性。我想要一种快速而肮脏的方式将此文本显示到表格中。所以,我正在做类似的事情......
NSDictionary *parameterValues = [[NSDictionary alloc] initWithObjectsAndKeys: sessionName, [NSNumber numberWithInt: 0], sessionDriver, [NSNumber numberWithInt: 1], sessionCar, [NSNumber numberWithInt: 2], sessionTrack, [NSNumber numberWithInt: 3], nil];
NSString *parameterString;
if([indexPath row] > 0) {
if([parameterValues objectForKey: [NSNumber numberWithInt: [indexPath row]]] == [NSNull null]) {
parameterString = [[NSString alloc] initWithFormat: @"Select a %@", [parameterNames objectAtIndex: [indexPath row]]];
} else{
parameterString = [[parameterValues objectForKey: [NSNumber numberWithInt: [indexPath row]]] name];
}
} else{
parameterString = [parameterValues objectForKey: [NSNumber numberWithInt: 0]];
if([parameterString isEqualToString: @""]) {
parameterString = @"Enter A Name";
}
}
这在我开始将会话作为实例变量传递之前有效,而不是跟踪特定的字符串、驱动程序、汽车和跟踪对象。由于 [[self session] driver], 将在传递新会话对象时返回 nil,因此不能使用字典对象。我现在就是这样做的……
//these come in handy, they're the object names (We can use KVC), and we can use them in the table titles
NSArray *parameterNames = [[NSArray alloc] initWithObjects: @"Name", @"Driver", @"Car", @"Track", nil];
//get the object for this row... (Name, Driver, Car, Track), and create a string to hold it's value..
id object = [session valueForKey: [parameterNames objectAtIndex: [indexPath row]]];
NSString *parameterValue;
NSLog(@"%@", [session name]);
//if this isn't the name row...
if(object != nil) {
//if the indexPath is greater than 0, object is not name (NSString)
if([indexPath row] > 0) {
parameterValue = [object name];
} else{
parameterValue = object;
}
} else{
//object doesn't exist yet... placeholder!
parameterValue = [@"Select a " stringByAppendingString: (NSString *)[parameterNames objectAtIndex: [indexPath row]]];
}
我要问的是……我这样做对吗?
谢谢, Matt - 核心数据新手:/
【问题讨论】:
标签: iphone cocoa-touch uitableview core-data uikit