【问题标题】:Customized checkbox for TableView gives NSManagedObject errorTableView 的自定义复选框给出 NSManagedObject 错误
【发布时间】:2014-03-15 19:38:57
【问题描述】:

我尝试为我的 TableView 设置一个自定义的附件复选框,但遇到了这个错误...

在 NSManagedObject 类型的元素上未找到读取字典元素的预期方法

我的 ViewController 的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    NSManagedObject *device = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        device = [searchResults objectAtIndex:indexPath.row];
    }
    else
    {
        device = [self.cart objectAtIndex:indexPath.row]; 
    }

    if (cell.accessoryView == nil) 
    {
        // Only configure the Checkbox control once.
        cell.accessoryView = [[Checkbox alloc] initWithFrame:CGRectMake(0, 0, 25, 43)];
        cell.accessoryView.opaque = NO;
        cell.backgroundColor = [UIColor clearColor];

        [(Checkbox*)cell.accessoryView addTarget:self action:@selector(checkBoxTapped:forEvent:) forControlEvents:UIControlEventValueChanged];

        [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"cartProductName"]]];

        [(Checkbox*)cell.accessoryView setChecked: [device[@"checked"] boolValue] ];//<-- Errorline            

        // Accessibility

        [self updatetAccessibilityForCell:cell];

        return cell;
    }

错误发生在setChecked: 行。

我的核心数据是:

购物车.h:

@interface Cart : NSManagedObject

@property (nonatomic, retain) NSString * cartProductName;

@end

购物车.m:

@implementation Cart

@dynamic cartProductName;

@end

我需要一个新的房产来完成吗?

我的 ViewController 的负责人:

#import "CartViewController.h"
#import "Checkbox.h"
#import "Cart.h"


@interface CartViewController ()
@property (strong) NSMutableArray *cart;

@end

@implementation CartViewController {
    NSArray *cartProducts;
    NSArray *searchResults;}
@synthesize searchbar;

抱歉,我是编程界的新手……

【问题讨论】:

    标签: ios uitableview nsmanagedobject


    【解决方案1】:

    [device[@"checked"] boolValue] 更改为[device.checked boolValue]

    您访问的不是NSDictionary,而是NSManagedObject

    【讨论】:

    • 感谢您的帮助,但这也不起作用:-/错误:在“NSManagedObject *”类型的对象上找不到属性“已检查”
    • 抱歉,您必须转换为实际的设备类型。 IE。 [((Device *)device).checked boolValue]
    • 当我将 [((Device *)device).checked boolValue] 复制到代码中时,我可以将 'Device' 替换为 'device,但是编译器需要一个表达式... :- /
    • 你知道需要哪种表达方式吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    相关资源
    最近更新 更多