Rahul
Rahul

Reputation: 211

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


}

It's crashing , and it's producing the mentioned ERROR. I've compiled it and it's crashing on

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

statement. Please Help, Thanks in advance :)

Upvotes: 0

Views: 4432

Answers (2)

Paresh Navadiya
Paresh Navadiya

Reputation: 38249

Check object obtained from array is string or array like using isKindOfClass

Here your object is NSString and you are assuming it as a NSArray

Make changes like this:

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

Upvotes: 2

iBhavin
iBhavin

Reputation: 1261

You stored arr_list in data than fetched it from data and stored into string.

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

So its not string type.You've to store it in appropriate type.

Upvotes: 0

Related Questions