xcode Objective-C 循环遍历数组并打印到屏幕

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13972334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:22:28  来源:igfitidea点击:

Objective-C loop through array and print to screen

objective-cxcode

提问by Patrick Reck

I am just starting on Objective-C and XCode today.

我今天刚刚开始使用 Objective-C 和 XCode。

I've made a

我做了一个

NSMutableArray

containing a bunch of strings.

包含一堆字符串。

I am looping through my array like this:

我像这样循环遍历我的数组:

for (NSString *test in array) {
}

Now, how do I manage to show each of these values on the screen, standing underneath each other? I am not sure which UI element would be proper, and how to actually use that element (I don't know what element it is yet, but I only have knowledge on TextField, Button and Label so far).

现在,我如何设法在屏幕上显示这些值中的每一个,并站在彼此的下方?我不确定哪个 UI 元素是合适的,以及如何实际使用该元素(我还不知道它是什么元素,但​​到目前为止我只了解 TextField、Button 和 Label)。

回答by Roland Keesom

Use a UILabel and set numberOfLines to 0 to have infinite lines.

使用 UILabel 并将 numberOfLines 设置为 0 以获得无限线。

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 200)];
myLabel.numberOflines = 0;
[self.view addSubview:myLabel];

NSString *testText = @"";
for (NSString *test in array) {
    testText = [testText stringByAppendingFormat:@"%@\n", text];
}
myLabel.text = testText;

回答by Dmitry Zheshinsky

You better make an UITableView number of rows at index path will be your [array count]; And at each cell, display [array objectAtIndex:indexPath.row]; If you need the whole code, tell me

你最好在索引路径上设置一个 UITableView 的行数将是你的 [array count]; 并在每个单元格处显示 [array objectAtIndex:indexPath.row]; 如果你需要完整的代码,告诉我

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                      reuseIdentifier:CellIdentifier];
    }

    cell.text = [array objectAtIndex:indexPath.row];
    return cell;
}