__NSCFConstantString objectAtIndex:]:发送到实例的无法识别的选择器

时间:2014-11-14 11:18:42

标签: ios objective-c iphone xcode uicollectionview

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cvCell";

    [self.collect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cvCell"];

    CollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];



    NSMutableArray *data = [arr_list objectAtIndex:indexPath.section];

    NSString *cellData = [data objectAtIndex:indexPath.row];

    [cell.lbl setText:cellData];

    return cell;


}

它崩溃了,它产生了上面提到的错误。我编译了它并且它崩溃了

 NSString *cellData = [data objectAtIndex:indexPath.row];

语句。请帮助,提前致谢:)

2 个答案:

答案 0 :(得分:2)

object obtainedarray检查string array,例如使用isKindOfClass

这里你的对象是NSString,你假设它是一个NSArray

进行如下更改:

 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cvCell";

    [self.collect registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cvCell"];

    CollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    id data = [arr_list objectAtIndex:indexPath.section];
    NSMutableArray *arrData = nil;
    if([data isKindOfClass:[NSString class]])
       NSLog(@"its string");
    else if([data isKindOfClass:[NSMutableArray class]])
    {
       arrData = (NSMutableArray *)data;
    }
    else
    {
       NSLog(@"its something else %@",data);
    }

    NSString *cellData = @"";
    if(arrData)
       cellData = [arrData objectAtIndex:indexPath.row];

    [cell.lbl setText:cellData];

    return cell;
}

答案 1 :(得分:0)

您将arr_list存储在data中,而不是从data获取并存储到字符串中。

  NSString *cellData = [data objectAtIndex:indexPath.row]

所以它不是字符串类型。您可以将它存储在适当的类型中。