繁体   English   中英

将字符串NSArray插入UILabel并以1计数。

[英]Insert NSArray of strings into UILabel and count by 1.

如何将字符串数组插入UILabel ,并让它们加载或计数为1? 我正在尝试将以下数组加载到UILabel

self.array = [NSArray arrayWithObjects:@"Testing 1",@"Testing 2",@"Testing 3", nil];

在这里,我使用的UIScrollView会自动从每张幻灯片更改为每张幻灯片:

 for (int i=1; i<=3; i++) {

        // create label
        UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,30)];
        text.text = [NSString stringWithFormat:@"%@", self.array];
        text.font=[UIFont fontWithName:@"HelveticaNeueLTStd-LtCn" size:15];
        text.textColor = [UIColor darkGrayColor];
        text.textAlignment =NSTextAlignmentCenter;
        // create imageView
        UIImageView *lblView = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 325, 280, 240)];

        // set label
        [lblView addSubview:text];

        // apply tag to access in future
        lblView.tag=i+1;

        // add to scrollView
        [scrMain addSubview:lblView];
    }

我有一个如何使用UIImageView的示例:

for (int i=1; i<=3; i++) {
        // create image
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"sti%02i.png",i]];
        // create imageView
        UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width + 23, 60, 280, 260)];
        // set scale to fill
        imgV.contentMode=UIViewContentModeScaleToFill;
        // set image
        [imgV setImage:image];
        // apply tag to access in future
        imgV.tag=i+1;
        // add to scrollView
        [scrMain addSubview:imgV];
    }

这些图像在我的项目中,并编号为sti01,sti02,依此类推...然后,每个图像都将其加一,然后将其加载到滚动视图中。 我希望这有帮助!

标签的y位置应根据索引进行调整。

您可以使用数组计数而不是固定的'3'。

您不需要两者之间的UIImageView。

您可以尝试以下方法:

for (int i = 0; i < self.array.count; i++) {

    // create label
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, i*30, self.view.frame.size.width,30)];
    label.text = [self.array objectAtIndex:i];
    label.font = [UIFont fontWithName:@"HelveticaNeueLTStd-LtCn" size:15];
    label.textColor = [UIColor darkGrayColor];
    label.textAlignment = NSTextAlignmentCenter;

    // add to scrollView
    [scrMain addSubview:label];
}

如果我对您的理解正确,则需要进行以下更改:

text.text = [NSString stringWithFormat:@"%@", [self.array objectAtIndex:i]];

这将从字符串数组中提取第i个对象,并将其设置为UILabel的文本。

为什么不遍历数组而不是尝试像这样将文本分配给整个数组:

for (int i=0; i<3; i++) {

    // create label
    UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,30)];
    text.text = [NSString stringWithFormat:@"%@", [self.array objectAtIndex:i]];
    text.font=[UIFont fontWithName:@"HelveticaNeueLTStd-LtCn" size:15];
    text.textColor = [UIColor darkGrayColor];
    text.textAlignment =NSTextAlignmentCenter;
    // create imageView
    UIImageView *lblView = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 325, 280, 240)];

    // set label
    [lblView addSubview:text];

    // apply tag to access in future
    lblView.tag=i+1;

    // add to scrollView
    [scrMain addSubview:lblView];
}

这样,您只需拉出一个字符串,将其分配为UILabel的文本,然后将其作为子视图添加到适当的imageView即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM