0

我正在尝试使用NSMutableDictionary. _coordinateRecords我在视图控制器的 viewDidLoad 函数中使用它创建了一个坐标网格(作为我的字典):

[self _loopOnCellWithIterator: (^(int iLooper, int jLooper)
{
    int cellWidth = 10;
    int space = 1;

    [_coordinateRecords setObject: [NSIndexPath indexPathForRow: iLooper inSection: jLooper] forKey: [NSValue valueWithPointer: redCell]];

    redCell = [[[UIView alloc] initWithFrame: CGRectMake(iLooper * (cellWidth + space) + 24.25,
                                                         jLooper * (cellWidth + space) + 110,
                                                         cellWidth, cellWidth)] init];
    _cells[iLooper][jLooper] = redCell;

    [redCell setBackgroundColor: [UIColor blueColor]];

    [[self view] addSubview: redCell];
})];

使用这个块:

- (void)_loopOnCellWithIterator: (void(^)(int iLooper, int jLooper))iterator
{
    for (int iLooper = 0; iLooper < kCellSize; iLooper++)
    {
        for (int jLooper = 0; jLooper < kCellSize; jLooper++)
        {
            iterator(iLooper, jLooper);
        }
    }
}

所以,我想知道如何调用我设置的字典中的对象。

4

1 回答 1

0

我不确定你在问什么,但有两件事可能会有所帮助:

  • 您可以使用键访问字典中的值。该方法-objectForKey:接受一个键并返回关联的对象。您应该知道键是什么,但还有一种方法可以提供包含所有键的数组。

  • 如果您很难从网格坐标映射到字典键,那么您可能使用了错误的数据结构。数组可能更合适,因为它很容易从坐标转换为索引。

于 2013-06-28T10:02:47.643 回答