【问题标题】:Why its Come [__NSCFString objectAtIndex:]: unrecognized selector sent to instance?为什么它来 [__NSCFString objectAtIndex:]: unrecognized selector sent to instance?
【发布时间】:2016-06-05 12:31:45
【问题描述】:

我正在创建 UITableview 并添加两个 UITableviewCell 自定义单元格(ProductCell 和 DescriptionCell)。我正在将数据显示到 UILabel 中。但我收到此错误([__NSCFString objectAtIndex:]: unrecognized selector sent to instance)并且 UITableviewCell 不显示数据。 请检查我的代码。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;


    [self.detailedTableview registerNib:[UINib nibWithNibName:@"ProductCell" bundle:nil] forCellReuseIdentifier:@"ProductCell"];

    [self.detailedTableview registerNib:[UINib nibWithNibName:@"DescriptionCell" bundle:nil] forCellReuseIdentifier:@"DescriptionCell"];

}

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

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

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.detailedTableview])
    {
        if (indexPath.section == 0)
        {
            return 396;
        }
        if (indexPath.section == 1)
        {
            //  return UITableViewAutomaticDimension;
            return 100;

        }
           }
    else
    {
        if (indexPath.section == 0)
        {
            return 396;
        }
        if (indexPath.section == 1)
        {
            // return UITableViewAutomaticDimension;
            return 100;
        }
    }
    return 0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifierproduct = @"ProductCell";
    static NSString *cellIdentifierDescription = @"DescriptionCell";

    if (indexPath.section == 0) {
        ProductCell *cellProduct =[tableView dequeueReusableCellWithIdentifier:cellIdentifierproduct];



        return cellProduct;
    }
    else if (indexPath.section ==1)
    {
        DescriptionCell *cellDes =[tableView dequeueReusableCellWithIdentifier:cellIdentifierDescription];

        NSString*urlString  = [NSString  stringWithFormat:@"http://54.254.171.25/android/bnm/api/servicesAPI.php?request=getProductDetails&pid=1"];
        NSURL *url=[NSURL URLWithString:urlString];
        NSData *data=[NSData dataWithContentsOfURL:url];
        NSError *error;
       NSDictionary *dataDictionary=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
       NSMutableArray *descArray =[dataDictionary objectForKey:@"pd_description"];
         NSLog(@"%@",descArray);
         cellDes.descriptionLbl.text = [descArray objectAtIndex:indexPath.row];

        return cellDes;
    }

    return nil;
}

请检查

【问题讨论】:

  • 为什么要在cellForRowAtIndexPath方法中下载数据并解析JSON?这需要完成一次,然后将生成的数据用作表格视图的数据源。
  • 这行NSLog(@"%@",descArray); 在崩溃前打印什么?
  • 在 descArray 中打印了这个(亚麻混纺材料。常规衣领。全袖。修身。纯色图案。桃色)
  • 请阅读一些关于模型-视图-控制器模式的内容。 controller 将数据加载到 model 并更新 view。您正在 view 中加载数据,这太可怕了。

标签: ios objective-c uitableview


【解决方案1】:

pd_description 是一个字符串。请记住,NSDictionary 与通用对象或id 一起使用。因此,当您将 NSString 分配给 NSArray 时,编译器不会发现问题。但是在运行时,您尝试从 String 对象调用 Array 的方法,这些方法在 NSString 定义中不存在,导致Unrecognized selector sent to instance。

pd_description: "Linen Blend Material.
 Regular Collar.
 Full Sleeves.
 Slim Fit.
 Solid Pattern.
 Peach Color",

解决此问题的一种方法是获取字符串并分离其组件。

NSString *pdDescription = [dataDictionary objectForKey:@"pd_description"];
NSMutableArray *descArray = [pdDescription componentsSeparatedByString:@"."];
NSLog(@"%@",descArray);
         cellDes.descriptionLbl.text = [descArray objectAtIndex:indexPath.row];

更新:删除换行符

NSString *pdDescription = [dataDictionary objectForKey:@"pd_description"];
pdDescription = [pdDescription stringByReplacingOccurrencesOfString:@"\r\n " withString:@""];
NSMutableArray *descArray = [pdDescription componentsSeparatedByString:@"."];
NSLog(@"%@",descArray);
cellDes.descriptionLbl.text = [descArray objectAtIndex:indexPath.row];

【讨论】:

  • 但在我的 tableviewcell 数据中没有显示。为什么它没有显示在 UILabel 中。
  • 那可能是换行的原因,让我更新一下答案。
【解决方案2】:

Desc 数组有一个indexpath,所以你的代码会崩溃。试试这段代码

cellDes.descriptionLbl.text = [dataDictionary objectForKey:@"pd_description"];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多