2012-12-20 27 views
1

我刚开始使用Objective-C和XCode。Objective-C遍历数组并打印到屏幕

我做了一个

NSMutableArray 

包含一串字符串。

我通过我的数组循环是这样的:

for (NSString *test in array) { 
} 

现在了,我怎么管理,以显示这些值的屏幕上,站在对方的下面?我不确定哪个UI元素是合适的,以及如何真正使用这个元素(我不知道它是什么元素,但​​我只有TextField,Button和Label的知识)。

回答

2

使用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; 
+0

可以在旅途中编辑行数吗?所以每次循环访问数组时,它都会添加一个新行? –

+0

这不是必需的。你需要设置标签的框架来创建尺寸 –

1

你最好做出的UITableView 数量在指数路径行的将是你的[数组数] 并在每个单元格处显示[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; 
} 
+0

我绝对会喜欢代码示例 –