2013-03-27 46 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; 

}