Chris
Chris

Reputation: 17

UITableView displaying

If u have array created like this

 quizArrayAnswers=[[NSMutableArray alloc] initWithObjects:
                  [NSMutableArray arrayWithObjects: @"blue", @"Red", @"Green",@"Yellow", nil],
                  [NSMutableArray arrayWithObjects: @"fred", @"jim", @"spud",@"tom", nil],
                  [NSMutableArray arrayWithObjects: @"albert", @"jim", @"spud",@"tom", nil],
                  //[NSMutableArray arrayWithObjects: @"emergency btn", @"fire alarm", @"doorbell",@"danger", nil],
                  [NSMutableArray arrayWithObjects: @"dog", @"rabbit", @"bird",@"flea", nil],nil];

how do u get the UITableView text label to display the first row of the first objects I'm trying this but no good all other table methods are done

   cell.textLabel.text = [quizArrayQuestions objectAtIndex:[indexPath row]];

thanks

Upvotes: 0

Views: 71

Answers (3)

FeifanZ
FeifanZ

Reputation: 16316

I'm not quite sure what you mean by "first row of the first objects." It could be what Thor is suggesting, in which case it'll grab the first object in the arrays for your first four table cells.

You could also do

cell.textLabel.text = [[quizArrayQuestions objectAtIndex:0] objectAtIndex:[indexPath row]];

which would get you the objects in the first array alone.

More generally speaking, you might use nested arrays if you're dealing with sections as well:

cell.textLabel.text = [[quizArrayQuestions objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]];

Regardless, with nested arrays, you'd call objectAtIndex: first to get the array at that index. Then you call objectAtIndex: again to get the element you want from within that array.


One thing to mention is that you want to avoid using mutable arrays unless you actually plan on modifying the data. Because you're hard-coding the values and these values are to be displayed in a tableview, I'm guessing that you don't intend for them to change. It's better for performance and for "style points" if you use non-mutable elements where possible.

Upvotes: 1

Paul.s
Paul.s

Reputation: 38728

I'm not 100% what you are trying to achieve.

Your current logic will return one of the four NSMutableArray objects. You will then need to decide which item from this NSMutableArray you want to display and perform another objectAtIndex: call.

So for example if you was on row 2 your current code would return this object

(
    albert, 
    jim,
    spud,
    tom
)

You would then need to grab one of the strings. It may look like this

NSArray *questions = [quizArrayQuestions objectAtIndex:[indexPath row]];
cell.textLabel.text = [questions objectAtIndex:indexOfQuestionText];

Upvotes: 1

Thor Frølich
Thor Frølich

Reputation: 664

You have nested arrays within an array. So you need to call the following instead:

cell.textLabel.text = [[quizArrayQuestions objectAtIndex:[indexPath row]] objectAtIndex: 0];

Upvotes: 0

Related Questions