开发者

IOS: NSMutableArray in Viewdidload

开发者 https://www.devze.com 2023-02-24 04:33 出处:网络
in my project I use a NSmutablearray that I f开发者_如何学编程ill with UIImageView in .m (viewDidLoad)

in my project I use a NSmutablearray that I f开发者_如何学编程ill with UIImageView in .m (viewDidLoad)

arrayView = [[NSMutableArray alloc] initWithObjects: image1, image2, image3, nil];

but in method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

when I write

int i = 1;
[[arrayView objectAtIndex:i] setImage:image];

there is an exception that say that my array is empty...why?


Please post the rest of viewDidLoad. I want to see how image1 is initialized. The line:

arrayView = [[NSMutableArray alloc] initWithObjects: image1, image2, image3, nil];

will return an empty array if image1 is nil. You should also protect your app from crashing by not making assumptions about the data source. You shouldn't crash if the array is empty, just display an empty tableView.

[EDIT]

I just read the last comment you made in the other answer. Sounds like your imageViews are created in IB. Make sure they are connected to the image1 etc outlets in IB.


Try this instead:

if (i < [arrayView count]) {
    UIImageView *imageView = [arrayView objectAtIndex:i];
    imageView.image = image;
}

Separating accessing the array element (by assigning it to an actual UIImageView object) and then assigning the new image may be helpful. I've seen cases where if you stack up too many operations things get confused, especially if you are dealing with objects of different types that may or may not have the selectors you're using.

Why your array is turning up empty is another issue. Initializing it in viewDidLoad seems right. You may need to add some "protection" (as above) in your table methods to avoid accessing an empty array. Then in method like viewWillAppear:, call reloadData.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号