这里是整个代码:iOS版 - 的cellForRowAtIndexPath代码说明
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"DetailCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:CellIdentifier];
NSUInteger sectionIndex = [indexPath section];
NSUInteger rowIndex = [indexPath row];
NSDictionary *section = [self.sections objectAtIndex:sectionIndex];
NSArray *rows = [section objectForKey:@"rows"];
NSDictionary *row = [rows objectAtIndex:rowIndex];
cell.textLabel.text = [row objectForKey:@"label"];
cell.detailTextLabel.text = [[self.myManagedObject valueForKey:[row objectForKey:@"key"]] description];
return cell;
}
我的问题是关于:
cell.textLabel.text = [row objectForKey:@"label"];
cell.detailTextLabel.text = [[self.myManagedObject valueForKey:[row objectForKey:@"key"]] description]; //This is NSDate
- 为什么采用两种不同的格式的代码:objectForKey & valueForKey?
- 为什么没有必要使用objectForKey调用self.myManagedObject?
- 描述的目的是什么?
objectForKey是你通常用来访问一个NSDictionary。 valueForKey用于“键值编码”,它是使用类似字典的语法访问非字典中字段的一般机制。对于“描述”,请查看[NSObject spec](https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#// apple_ref/OCC/intfm/NSObject的/描述)。 – 2013-03-27 20:54:16