How do you add every number between 1 and a changing number to a NSMutableArray (so that it can be shown in a UITableView)?
For example, if the changing number at the moment is 8, then the array should contain: 1, 2, 3, 4, 5, 6, 7, 8
. Thanks.
How do you add every number between 1 and a changing number to a NSMutableArray (so that it can be shown in a UITableView)?
For example, if the changing number at the moment is 8, then the array should contain: 1, 2, 3, 4, 5, 6, 7, 8
. Thanks.
i recommend the following approach (not needing an array). for your a 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;
}
resulting in a table with 1 section, a rows, and each row will have a label with the number 1 to a on it.
sebastian
Something like:
int number = 8;
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:number];
for (int i=1; i<=number; i++) {
[mutableArray addObject:[NSString stringWithFormat:@"%i", i]]
}
NSLog(@"%@", [mutableArray description]);
- (NSMutableArray*)arrayForNumber:(int)number {
NSMutableArray* array = [NSMutableArray array];
for (int i = 1; i <= number; i++) {
[array addObject:[NSNumber numberWithInt:i]];
}
return array;
}