-1

如何将 1 和变化的数字之间的每个数字添加到 NSMutableArray(以便它可以显示在 UITableView 中)?

例如,如果此刻变化的数字是 8,那么数组应该包含:1, 2, 3, 4, 5, 6, 7, 8。谢谢。

4

3 回答 3

4

我推荐以下方法(不需要数组)。为你的数字

-numberOfSectionsInTableView..{
    return 1;
}

-numberOfRowsInSection..{
    return a;
}

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

生成一个包含 1 个部分、一个行的表,并且每行将有一个标签,其上带有数字 1 到 a。

塞巴斯蒂安

于 2012-04-07T20:30:30.047 回答
2

就像是:

int number = 8;
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:number];
for (int i=1; i<=number; i++) {
    [mutableArray addObject:[NSString stringWithFormat:@"%i", i]]
}
NSLog(@"%@", [mutableArray description]);
于 2012-04-07T20:25:40.360 回答
1

- (NSMutableArray*)arrayForNumber:(int)number {
    NSMutableArray* array = [NSMutableArray array];

    for (int i = 1; i <= number; i++) {
        [array addObject:[NSNumber numberWithInt:i]];
    }

    return array;
}
于 2012-04-07T20:25:55.350 回答