IOS - UIScrollView does not update its content, and some UIView disappear after reboot

I have a UITableViewController with a UIScrollView inside a UIView as its title. UIScrollView used to display image slide shows:

 UITableView -UITableViewHeaderView -UIView -UIScrollView -UIImageView ... -UIPageControl 

Each image has a subitem with its name and description:

 UIImageView -UIView -UILabel -UILabel 

When I need to update my tableView , the delegate method is called, which reloads the data in the tableView and calls the addImages method:

 - (void)eventsUpdated { if ([self respondsToSelector:@selector(setRefreshControl:)]) [self.refreshControl endRefreshing]; [self.tableView reloadData]; [self addImages]; } 

This is how I add my images:

 - (void)addImages [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; for (int i = 0; i < images.count; i++) { CGRect frame; frame.origin.x = self.scrollView.frame.size.width * i; frame.origin.y = 0; frame.size = self.scrollView.frame.size; UIImageView *subview = [self createImageViewForEvent:[images objectAtIndex:i] inFrame:frame]; subview.frame = frame; [self.scrollView addSubview:subview]; } self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*images.count, scrollView.frame.size.height); pageControll.currentPage = 0; pageControll.numberOfPages = images.count; } - (UIImageView *)createImageViewForEvent:(Event *)event inFrame:(CGRect)frame { UIImage *image; NSString *imageName = [event.imageName lastPathComponent]; NSString *bundleFilePath = [[NSBundle mainBundle] pathForResource:[imageName stringByDeletingPathExtension] ofType:[imageName pathExtension]]; image = [UIImage imageWithContentsOfFile:bundleFilePath]; UIImageView *output = [[UIImageView alloc] initWithImage:image]; output.frame = frame; UIView *descriptionView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height*0.7, frame.size.width, frame.size.height*0.3)]; descriptionView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]; descriptionView.alpha = 1.0; UILabel *header = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, frame.size.width-20, 12)]; header.textColor = [UIColor colorWithRed:210/256.0 green:217/256.0 blue:231/256.0 alpha:1.0]; header.font = [UIFont fontWithName:@"Helvetica" size:17]; header.backgroundColor = [UIColor clearColor]; header.lineBreakMode = UILineBreakModeTailTruncation; header.numberOfLines = 1; UILabel *description = [[UILabel alloc] initWithFrame:CGRectMake(10, 22, frame.size.width-20, 28)]; description.textColor = [UIColor colorWithRed:255/256.0 green:255/256.0 blue:255/256.0 alpha:1.0]; description.font = [UIFont fontWithName:@"Helvetica" size:12]; description.backgroundColor = [UIColor clearColor]; description.lineBreakMode = UILineBreakModeTailTruncation; description.numberOfLines = 2; header.text = event.title; [descriptionView addSubview:header]; description.text = event.shortDesription; [descriptionView addSubview:description]; [output addSubview:descriptionView]; return output; } 

After the first start everything works fine, but if I try to reload my tableView and call addImages again, all UILabels will disappear, only UIImageView and UIView will be visible.

UPD:

I noticed that my UILabels appear after some time after about 30 seconds. I have never experienced something like this before

UPD 2:

After reloading the contents of tableView and scrollview , scrollView's not updated immediately, only after I start scrolling

+7
source share
2 answers

I reproduced a sample project from the code that you posted, and did not find any update problems on the shortcuts or on any other component. This makes me think that the problem is not with the code you posted, but rather elsewhere.

Usually, when you see such a delay, this is due to the fact that some code that should not be executed in parallel is executed in parallel.

I suggest you check how and when you make your eventsUpdated call. You must make sure that the call is made in the main thread, and only after your array of events has completed the update. To verify this, you can add the following line to eventsUpdated :

NSLog(@"Current: %@, main: %@", [NSThread currentThread], [NSThread mainThread]);

Please publish a magazine! :-)

I would suggest you add to your question code from a method that calls eventsUpdated , as it might also help.

+5
source

First, never do something like

  [scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 

There are some internal views in the scroll view (for example, scroll indicators), so you delete them, which can cancel them and cause strange crashes at any time later.

As for your problem - try calling the bringSubviewToFront: method of your UIImageView to get your tags before everything else.

0
source

All Articles