【问题标题】:NSCF type value into UITableViewCellNSCF 类型值到 UITableViewCell
【发布时间】:2011-09-02 09:34:50
【问题描述】:

我正在尝试在 UITableViewCell 中的 cellAtRowIndexPath 方法中设置数组值,但它给出了异常

[NSCFType objectAtIndex:]:无法识别的选择器发送到实例 0x445e980' 2011-05-26 13:34:12.812 地址簿[2603:20b] 堆栈:( 9180251,

“人”数组中的值属于“NSCF”类型。我想,它必须转换为 NSString。我将如何在 tableviewcell 中显示“人”数组的值?这是我的代码:

- (void)viewDidLoad { [超级视图DidLoad]; ABAddressBookRef addressBook = ABAddressBookCreate(); people = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); NSLog(@"人....%@",人); // 取消注释以下行以在此视图控制器的导航栏中显示一个编辑按钮。 // self.navigationItem.rightBarButtonItem = self.editButtonItem; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 静态 NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 如果(细胞 == 零){ cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // 设置单元格... cell.text = [人 objectAtIndex:indexPath.row]; 返回单元格; }

【问题讨论】:

  • 能否给出地址数组中对象的结构?
  • 另外,地址真的是一个数组吗?
  • 什么是people?它似乎不包含字符串。
  • @deepak : people 数组包含人....( , , , , )

标签: iphone


【解决方案1】:

objectAtIndex: 是您在 NSString(地址)处调用的 NSArray 实例方法

编辑:如果您确定人们包含 NSArray 或其子类,则进行枚举而不是 NSString。您不能将 NSArray 类的实例方法用于 NSString 实例。因此,它无法识别。

【讨论】:

    【解决方案2】:

    地址是一个字符串(没有objectAtIndex:方法)
    只需删除 for 循环,使用:

    cell.text = [people objectAtIndex:indexPath.row];
    

    【讨论】:

    • 给出异常“-[NSCFType isEqualToString:]:无法识别的选择器发送到实例 0x445e760 2011-05-26 14:01:07.612 addressBook[2753:20b] *** 由于未捕获的异常而终止应用程序” NSInvalidArgumentException',原因:'*** -[NSCFType isEqualToString:]:无法识别的选择器发送到实例 0x445e760'
    • 你在使用 cell.text = [people objectAtIndex:indexPath.row];
    • 请说明“人”的数据类型是什么?
    • cell.text = [数组 objectAtIndex:indexPath.row];
    • 我从这行代码中获取人员数组中的值 ABAddressBookRef addressBook = ABAddressBookCreate(); people = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
    【解决方案3】:

    people 数组是 ABPerson 对象而不是字符串的数组。因此,您不能将其分配为字符串。在将其分配给单元格的textLabel 之前,您必须处理记录并获取名称。

    ABRecordRef person = (ABRecordRef)[contacts objectAtIndex:0];
    CFStringRef nameString = ABRecordCopyCompositeName(person);
    cell.textLabel.text = (NSString*)nameString;
    CFRelease(nameString);
    

    【讨论】:

    • 我在通讯录中有很多联系人,所以我对您的代码做了一些改动。事实上,我需要在唯一的行中显示个人联系人。
    猜你喜欢
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多