簡體   English   中英

__NSCFConstantString objectAtIndex:]:無法識別的選擇器已發送到實例

[英]__NSCFConstantString objectAtIndex:]: unrecognized selector sent to instance

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

聲明。 請幫助,在此先感謝:)

檢查從array obtained objectstring還是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;
}

您將arr_list存儲在data不是從data獲取並存儲到字符串中。

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

因此它不是字符串類型。您必須將其存儲在適當的類型中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM