How to add 1 to a number in NSMutableArray

How do you add every number between 1 and a changing number in NSMutableArray (so that it can be displayed in a UITableView)?

For example, if the changing number at the moment is 8, the array should contain: 1, 2, 3, 4, 5, 6, 7, 8 . Thanks.

-1
source share
3 answers

Sort of:

 int number = 8; NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:number]; for (int i=1; i<=number; i++) { [mutableArray addObject:[NSString stringWithFormat:@"%i", i]] } NSLog(@"%@", [mutableArray description]); 
+2
source

I recommend the following approach (not needing an array). for your numbers

 -numberOfSectionsInTableView..{ return 1; } -numberOfRowsInSection..{ return a; } -cellForRowAtIndexPath..{ UITableViewCell* cell = ... UILabel *label = ... [label setText:[NSString stringWithFormat:@"%d",indexPath.row+1]]; [cell addSubView:label]; return cell; } 

the result is a table with 1 section, rows, and each row will have a label from number 1 to a.

Sebastian

+4
source
 - (NSMutableArray*)arrayForNumber:(int)number { NSMutableArray* array = [NSMutableArray array]; for (int i = 1; i <= number; i++) { [array addObject:[NSNumber numberWithInt:i]]; } return array; } 
+1
source

Source: https://habr.com/ru/post/922029/


All Articles