1

Very basic question :)

I have to initialize an array in objective-c. I will further use that array value in table view. I have a listing of chapters and I have written it as hardcoded like below.

    NSArray *tableList;

tableList = [[NSArray alloc]initWithObjects:@"Chapter 1",@"Chapter 2",@"Chapter 3",@"Chapter 4",@"Chapter 5",nil];

But now I have a indexed array which is keeping chapters details. below is the code of array which is holding value.

 extern NSArray *wallvalue;

    for (NSDictionary *chapter in wallvalue) {
        NSString *chapterName = [person objectForKey:@"chapters"];
        if([chapterName length] >0)
        {

            NSLog(chapterName);
        }
    }

Now I want to show these chapterName in my tablelist. How will I do this ?

Thanks in advance.

2 Answers 2

3
NSMutableArray *tableList = [[NSMutableArray alloc] initWithCapacity:[wallvalue count]];
for (NSDictionary *chapter in wallvalue) {
    NSString *chapterName = [person objectForKey:@"chapters"];
    if([chapterName length] >0)
    {
        [tableList addObject:chapterName];
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

@Joe, Thanks for reply. But I am seeing blank on table view. I think strings are not attaching with tablelist. Please check once.
Shouldn't be empty if you were able to log the chapter name before. Something is nil somewhere, also check Deepak's edit for filtering using a predicate.
@Joe. Yes you right it was being empty. Now I am getting an exception. I think this is happening due to NULL statement.
You need to do an NSNull check on chapterName. if(![[NSNull null] isEqual:chapterName] && [chapterName length]) of if you have control over the dictionary just dont add the key if it is going to be null.
I don't think that is the case. The predicate handles [NSNull null] instance.
|
0

Shouldn't you just be doing,

cell.textLabel.text = [[wallvalue objectAtIndex:indexPath.row] objectForKey:@"chapters"];

EDIT If you are interested in only those chapters which have a name, then filter all objects that don't have a proper name and then do the thing above,

NSArray * filteredChapters = [wallvalue filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"chapters LIKE '?*'"]];

[..]

cell.textLabel.text = [[filteredChapters objectAtIndex:indexPath.row] objectForKey:@"chapters"];

4 Comments

Ajay is checking the length of the chapter so another array will be needed to pass the correct number of rows in the case of using a UITableView.
Yeah I updated it so that we can filter all those dictionaries that have a chapter name.
There we are gotta love predicates.
Yes Now I am getting but I am getting on excepetion which is -[NSNull length]: unrecognized selector sent to instance 0xe515e8 2011-06-22 19:36:20.183 FEngine[2636:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0xe515e8'. Is it not cheking length properly ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.