为什么它来[__ NSCFString objectAtIndex:]:无法识别的选择器发送到实例?

时间:2016-05-25 17:38:47

标签: ios objective-c uitableview

我正在创建UITableview并添加两个UITableviewCell自定义单元格(ProductCell和DescripotionCell)。我正在向UILabel显示数据。但我得到此错误([__ NSCFString objectAtIndex:]:无法识别的选择器发送到实例)和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;
}

请检查

2 个答案:

答案 0 :(得分:1)

Desc数组有一个indexpath,因此您的代码将崩溃。试试这段代码

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

答案 1 :(得分: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];