1

这是整个代码:

- (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. 描述的目的是什么?
4

2 回答 2

2

为什么这段代码使用两种不同的格式:objectForKey 和 valueForKey?

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

self.myManagedObject是一个NSManagedObject。没有objectForKey:办法NSManagedObject

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

描述的目的是什么?

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

它们就像一条线。您可以将其拆分为多行,如下所示

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

cell.detailTextLabel.text = customclassObj.description;
于 2013-03-27T20:56:49.873 回答
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)];
    name.text=@"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)];
    add.text=@"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;

}
于 2015-05-06T05:58:31.420 回答