2013-03-27 45 views
1

这里是整个代码: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 
  1. 为什么采用两种不同的格式的代码:objectForKey & valueForKey?
  2. 为什么没有必要使用objectForKey调用self.myManagedObject?
  3. 描述的目的是什么?
+0

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

回答

2

为什么在用两种不同的格式的代码:objectForKey & valueForKey?

为什么不需要调用self.myManagedObject与 objectForKey?

self.myManagedObjectNSManagedObjectNSManagedObject没有objectForKey:方法。

rowNSDictionary类型。 objectForKey:是一种字典方法。

描述的目的是什么?

[self.myManagedObject valueForKey:[row objectForKey:@"key"]]应当返回一个对象,该对象将包含一个NSString类型属性作为description

他们只是一条线。您可以将其拆分为多行如下

YourCustomClass *customclassObj = [self.myManagedObject valueForKey:[row objectForKey:@"key"]]; 

cell.detailTextLabel.text = customclassObj.description; 
+0

谢谢。问题2:为什么没有必要使用objectForKey调用self.myManagedObject? – user1107173 2013-03-27 21:02:12

+0

什么是self.myManagedObject数据类型?字典或其他东西? – 2013-03-27 21:11:56

+0

在此上下文中使用'valueForKey:'允许间接级别应用于通用类对象(实现KVC)。字典(行)包含一个值,该值指定要在'self.managedObject'上访问哪个属性。所以如果字典中的值是'@“thingy”',你可以把第二行看作是访问'[[self.managedObject thingy]描述]',其中thingy是类的某个属性。 – sapi 2013-03-27 21:16:07

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     //create new cell 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 



    [tableView setRowHeight: 50.00]; 

    UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, 70, 30)]; 
    [email protected]"Ajay"; 
    [cell.contentView addSubview:name]; 
    [name release]; 

    // cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    //cell.contentView.backgroundColor=[UIColor whiteColor]; 
    UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(80, 5, 70, 30)]; 
    //UIButton *btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    //btn1.frame=CGRectMake(80, 5, 40, 30); 
    [btn1 setTitle:@"Name" forState:UIControlStateNormal]; 
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    //[btn1 addTarget:self action:@selector(Nam:) forControlEvents:UIControlEventTouchUpInside]; 
    btn1.backgroundColor=[UIColor greenColor]; 
    [[btn1 layer]setCornerRadius:8.0f]; 
    [[btn1 layer]setBorderWidth:1.0f]; 
    [cell.contentView addSubview:btn1]; 
    [btn1 release]; 

    UILabel *add=[[UILabel alloc]initWithFrame:CGRectMake(10, 56, 70, 50)]; 
    [email protected]"Biliya"; 
    [cell.contentView addSubview:add]; 
    [add release]; 

    // cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    //cell.contentView.backgroundColor=[UIColor whiteColor]; 
    UIButton *btn2=[[UIButton alloc]initWithFrame:CGRectMake(80, 56, 70, 30)]; 
    //UIButton *btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    //btn1.frame=CGRectMake(80, 5, 40, 30); 
    [btn2 setTitle:@"Address" forState:UIControlStateNormal]; 
    [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    //[btn1 addTarget:self action:@selector(Nam:) forControlEvents:UIControlEventTouchUpInside]; 
    btn2.backgroundColor=[UIColor greenColor]; 
    [[btn2 layer]setCornerRadius:8.0f]; 
    [[btn2 layer]setBorderWidth:1.0f]; 
    [cell.contentView addSubview:btn2]; 
    [btn2 release]; 




    return cell; 

}