如何通过跳过每1个索引来创建一个数组

时间:2013-04-18 19:13:24

标签: objective-c arrays

嗨有没有办法通过跳过每1个索引来制作一个数组 假设我有一个包含这样的对象的数组:

NSArray *array =[NSArray alloc] initWithObjects:@"id",@"name",@"id",@"name",@"id",@"name"..

......等等, 我只能通过 ID 对象和/或 NAMES 来制作数组。 我坚持这个,我真正想要实现的是我希望id在tableview副标题中,并在tableview标题上命名。 有什么建议吗?

编辑:感谢大家的帮助,已经得到了答案,我无法通过NSDictionary,因为我从.php获取对象。不管怎样,谢谢你!

5 个答案:

答案 0 :(得分:2)

您有两个选择

第一个(天真的):

cell.subtitle.text = array[indexPath.row*2];
cell.title.text = array[indexPath.row*2+1];

无论如何,更正确的选择是使用NSDictionary s

的数组
NSArray *array = [NSArray alloc] initWithObjects:@{
    @"id": @"theId", @"name" : @"theName"},
    @"id": @"anotherId", @"name" : @"anotherName"},
    ...];

然后

cell.subtitle.text = array[indexPath.row][@"id"];
cell.title.text = array[indexPath.row][@"name"];

答案 1 :(得分:2)

您可以通过创建一个字典对象数组轻松实现。

NSArray *array = @[@{@"Id":1, @"Name":@"UserName1"},@"Id":2, @"Name":@"UserName2"}];

并在cellForRowAtIndexPath:方法

NSDictionary *user = self.array[indexPath.row];

cell.textLabel.text = user[@"Id"];
cell.detailLabel.text = user[@"Name"];

答案 2 :(得分:1)

为什么不从头开始使用2个数组?如果你必须拆分数组,一个简单的方法就是在偶数和奇数上进行。

NSArray *array =[[NSArray alloc] initWithObjects:@"id",@"name",@"id",@"name",@"id",@"name",nil];

NSArray *ids = [array objectsAtIndexes:[array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    return ((idx % 2) == 0);
}]];

NSArray *names = [array objectsAtIndexes:[array indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    return ((idx % 2) != 0);
}]];

答案 3 :(得分:1)

如果你想在两个不同的数组中分隔id和名称,你可以迭代原始数组,并使用这样的mod运算符.-

NSArray *array = [[NSArray alloc] initWithObjects:@"id",@"name",@"id",@"name",@"id",@"name", nil];
NSMutableArray *ids = [[NSMutableArray alloc] init];
NSMutableArray *names = [[NSMutableArray alloc] init];

for (int i = 0; i < array.count; i ++) {
    if (i % 2 == 0) {
        [ids addObject:[array objectAtIndex:i]];
    } else {
        [names addObject:[array objectAtIndex:i]];
    }
}

答案 4 :(得分:-1)

许多答案只会建议使用其他一些建议,如果可以的话。但假设你有这样一个数组,并且需要这样做并不难。您可以将数组处理成两个 - 可能需要大量复制;或者您可以简单地查看数组 - 在访问时间内进行复制的smidgin。这是JASArray ,绝对没有检查代码(由您自己决定):

@interface JASArray : NSArray

- (id) initWith:(NSArray *)baseArray jumpTo:(NSUInteger)jumpTo strideBy:(NSUInteger)strideBy;

@end

要覆盖NSArray,您只需要实施countobjectAtIndex:

@implementation JASArray
{
   NSUInteger jump;
   NSUInteger stride;
   NSArray *base;
}

- (id) initWith:(NSArray *)baseArray jumpTo:(NSUInteger)jumpTo strideBy:(NSUInteger)strideBy
{
   self = [super init];
   if (self)
   {
      base = baseArray;
      jump = jumpTo;
      stride = strideBy;
   }
   return self;
}

- (NSUInteger) count
{
   return 1 + (base.count - jump - 1) / stride;
}

- (id) objectAtIndex:(NSUInteger)index
{
   return [base objectAtIndex:(jump + index * stride)];
}

@end

现在进行测试,注意有目的的不匹配对作为测试:

NSArray *array =@[ @"id1", @"name1", @"id2", @"name2", @"id3", @"name3", @"id4" ];
JASArray *ids = [[JASArray alloc] initWith:array jumpTo:0 strideBy:2];
JASArray *names = [[JASArray alloc] initWith:array jumpTo:1 strideBy:2];
NSLog(@"%@\n%@\n%@", array, ids, names);

输出:

JumpAndStride[10480:303] (
    id1,
    name1,
    id2,
    name2,
    id3,
    name3,
    id4
)
(
    id1,
    id2,
    id3,
    id4
)
(
    name1,
    name2,
    name3
)

HTH