如何为每个页面更改 UIScrollview 中的标签文本 不同的文本? [英] How to change the label text in UIScrollview for each page Different text?

查看:24
本文介绍了如何为每个页面更改 UIScrollview 中的标签文本 不同的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想在 UIScrollView 中的每个图像上显示不同的标签文本.这是我的代码.

Hi in my app i want to show label text different at each image in UIScrollView. Here is my code.

for (int i=0; i<imageArray.count; i++)
{
    NSString *str = [imageArray objectAtIndex:i];
    NSString *bannerImageURL = [NSString stringWithFormat:@"http://url.com", str];
    CGFloat myOrigin = i * self.view.frame.size.width;

 UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width,imgScrollView.frame.size.height)];
    myImageView.contentMode = UIViewContentModeScaleToFill;
    myImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:bannerImageURL]]];
 imgScrollView.contentSize = CGSizeMake(self.view.frame.size.width * imageArray.count,                                      self.view.frame.size.height);

CGPoint scrollPoint = CGPointMake(0, 0);
[imgScrollView setContentOffset:scrollPoint animated:YES];
imgScrollView.delegate = self;
[imgScrollView addSubview:myImageView];
}

这是我的委托方法.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
 {
  [imgScrollView setContentOffset: CGPointMake(imgScrollView.contentOffset.x,0)];
 }
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
NSLog(@"Scrolling - You are now on page %i",page);
}

我想将一个数组传递给该标签,还有一件事如何将页面控制器添加到此代码中?请帮忙.

I want to pass an array to that label and one more thing how to add page controller to this code? Please Help.

推荐答案

我不太确定您在上述问题中指的是什么标签.我假设您在标头中定义了一个标签 UILabel * _label 和一个数组 NSArray * _namesArray.

I'm not really sure what label you are referring to in the above question. I'll assume you have a label UILabel * _label and an array NSArray * _namesArray defined in your header.

我已尝试稍微清理您的代码.试试这个:

I've tried to clean up your code a little bit. Try this:

for(int index = 0; index < [imageArray count]; index++){
  NSString * string = [imageArray objectAtIndex:index];
  NSString * bannerImageURL = [NSString stringWithFormat:@"http://url.com/%@", string]; // You weren't using the string in your formatter, so I added it to the end.
  CGFloat myOrigin = index * self.view.frame.size.width;

  UIImageView * myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(myOrigin, 0.0f, self.view.frame.size.width, imageScrollView.frame.size.height)];
  myImageView.contentMode = UIViewContentModeScaleToFill;
  myImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:bannerImageURL]]];
  [imageScrollView addSubview:myImageView];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * imageArray.count,                                      self.view.frame.size.height);
[imageScrollView setContentOffset:CGPointZero animated:YES];
imageScrollView.delegate = self;

那么对于委托方法,您需要尝试:

Then for the delegate methods, you'll want to try:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView; {
  CGFloat pageWidth = scrollView.frame.size.width;
  int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
  _label.text = [_namesArray objectAtIndex:page];
  NSLog(@"Scrolling - You are now on page %i, and the name for the label is: %@", page, [_namesArray objectAtIndex:page]);
}

希望有帮助!

这篇关于如何为每个页面更改 UIScrollview 中的标签文本 不同的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆