Patrick Reck
Patrick Reck

Reputation: 303

Objective-C loop through array and print to screen

I am just starting on Objective-C and XCode today.

I've made a

NSMutableArray

containing a bunch of strings.

I am looping through my array like this:

for (NSString *test in array) {
}

Now, how do I manage to show each of these values on the screen, standing underneath each other? I am not sure which UI element would be proper, and how to actually use that element (I don't know what element it is yet, but I only have knowledge on TextField, Button and Label so far).

Upvotes: 1

Views: 6022

Answers (2)

Dmitry Zheshinsky
Dmitry Zheshinsky

Reputation: 569

You better make an UITableView number of rows at index path will be your [array count]; And at each cell, display [array objectAtIndex:indexPath.row]; If you need the whole code, tell me

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                      reuseIdentifier:CellIdentifier];
    }

    cell.text = [array objectAtIndex:indexPath.row];
    return cell;
}

Upvotes: 1

Roland Keesom
Roland Keesom

Reputation: 8288

Use a UILabel and set numberOfLines to 0 to have infinite lines.

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 200)];
myLabel.numberOflines = 0;
[self.view addSubview:myLabel];

NSString *testText = @"";
for (NSString *test in array) {
    testText = [testText stringByAppendingFormat:@"%@\n", text];
}
myLabel.text = testText;

Upvotes: 2

Related Questions