动态表视图数据源

时间:2011-12-21 12:16:18

标签: iphone objective-c uitableview nsmanagedobject

您好我正在做一个应用程序,其中我使用动态视图表,我有NSManged对象的问题,任何人都可以找出问题出在哪里。

找到以下代码供您参考。

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return ([self.controlSelections count]);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"plainCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    id controlOption = [self.controlSelections objectAtIndex:indexPath.row];
    NSString *option = nil;

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    if ([controlOption isKindOfClass:[NSString class]])
    {
        option = (NSString *)controlOption;
        cell.textLabel.text = option;
    }
    else if ([controlOption isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *optionDict = (NSDictionary *)controlOption;
        option = [optionDict valueForKey:self.listKey];
        cell.textLabel.text = option;
    }
    else if ([controlOption isKindOfClass:[NSManagedObject class]])
    {
        NSManagedObject *context=[self NSManagedObject];
        NSManagedObject  *optionData = (NSManagedObject  *)controlOption;
        option = [optionData valueForKey:self.listKey];
        cell.textLabel.text = option;
    }

    return cell;
}

问题在于以下代码中没有采用NSMangedObject:

else if ([controlOption isKindOfClass:[NSManagedObject class]])
        {
            NSManagedObject *context=[self NSManagedObject];
            NSManagedObject  *optionData = (NSManagedObject  *)controlOption;
            option = [optionData valueForKey:self.listKey];
            cell.textLabel.text = option;
        }

任何帮助将不胜感激。提前谢谢。

2 个答案:

答案 0 :(得分:1)

在Prefix.pch文件中导入

答案 1 :(得分:0)

在这些方面尝试一些事情 -

else if ([controlOption isKindOfClass:[NSManagedObject class]])
{
    NSManagedObjectContext *context=[self managedObjectContext]; //I guess you have MOC somewhere.

    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:[optionData valueForKey:self.listKey] inManagedObjectContext: context];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];

    NSError *error;
    NSArray *objects = [context executeFetchRequest:request error:&error];

    cell.textLabel.text = [objects objectAtIndex:0]; //for eg   
}