2

试图解决一些 iOS 设备上发生的崩溃,并结合 Apple 的建议“不要导致分配高峰”。如何更改此代码以不立即发生?

for (Item *item in self.items) {
        ItemView *itemView = [[ItemView alloc] initWithFrame:CGRectMake(xPos, kYItemOffsetIphone, kItemWidthIphone, kItemHeightIphone) ];

        itemView.delegate = self;
        [itemView layoutWithData:item]; //this just adds an imageView and button
        [self.scrollView addSubview:itemView];
        xPos += kXItemSpacingIphone;
    }

self.items 数组中有大约 20 个对象,用于构建 20 个 ItemView。同样,有没有办法让这段代码不那么“分配密集”?

4

1 回答 1

1

I personally do something along the lines of:

  1. Make my view controller the delegate of the scroll view (if you do this in code, you have to modify your view controller's .h to say that it conforms to UIScrollViewDelegate).

  2. Define a scrollViewDidScroll method that (a) determines the frame of the visible portion of the scroll view; (b) determine which of the subviews intersect with that visible portion; (c) load the items that are visible, and unload the ones that aren't.

So, for example, it might look something like the following:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // Determine the frame of the visible portion of the scrollview.

    CGRect visibleScrollViewFrame = scrollView.bounds;
    visibleScrollViewFrame.origin = scrollView.contentOffset;

    // Now iterate through the various items, remove the ones that are not visible,
    // and show the ones that are.

    for (Item *itemObject in self.itemCollection)
    {
        // Determine the frame within the scrollview that the object does (or 
        // should) occupy.

        CGRect itemObjectFrame = [self getItemObjectFrame:itemObject];

        // see if those two frames intersect

        if (CGRectIntersectsRect(visibleScrollViewFrame, itemObjectFrame))
        {
            // If it's visible, then load it (if it's not already).
            // Personally, I have my object have a boolean property that
            // tells me whether it's loaded or not. You can do this any
            // way you want.

            if (!itemObject.loaded)
                [itemObject loadItem];
        }
        else
        {
            // If not, go ahead and unload it (if it's loaded) to conserve memory.

            if (itemObject.loaded)
                [itemObject unloadItem];
        }
    }
}

That's the basic idea. You can certainly optimize this logic based upon your app's particular design, but this is how I generally do it.

于 2012-10-18T21:59:42.817 回答