无法识别的选择器发送到实例

无法识别的选择器发送到实例

我正在创建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;
}

请检查

最佳答案

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];

关于ios - 为什么将其[__NSCFString objectAtIndex:]:无法识别的选择器发送到实例?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37444155/

10-14 05:27