在XCode中将单元格添加到表视图(UITableView) [英] Adding cells to table view (UITableView) in XCode
问题描述
我是X-Code的新手,刚开始使用我的第一个应用程序。我正在使用情节提要。在导航控制器场景中,我添加了一个具有两个单元格的MasterViewController,这导致了两个DetailViewControllers(Detail1和Detail 2)。
I'm new to X-Code and have just started working on my first app. I'm using Storyboards. To the Navigation Controller scene I've added a MasterViewController with two cells, which leads to two DetailViewControllers (Detail1 & Detail 2).
- 列数= 1
- 列数= 2
我已添加
但是当我运行应用程序时,我只能看到第一个单元格x2。
But when I run the app, I can only see the first cell x2.
当我查看MasterViewController.m时,可以看到第一个单元格的代码,但是我不知道如何插入第二个单元格,等等!
When I look in the MasterViewController.m I can see the code for the first cell, but I don't understand how to insert the second cell etc!
我知道解决方案很简单,并且我一直在寻找答案三天,我觉得很愚蠢,但是我现在确实需要一些帮助,
I know the solution is easy and I've been looking for the answer for 3 days now, I feel stupid but I really need some help now,,
有人可以帮助我了解如何在代码中添加更多单元格,也许还可以更多地了解Table View代码?
Can someone help me understand how to add more cells in the code, and maybe understand the Table View Code a bit more?
这到目前为止是表视图的代码:
This is the code for the table view so far:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Formal";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath][withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
@end
推荐答案
假设您有一个名为arr的数组。
Lets say you have an array called arr.
然后,当调用numberOfRowsInSection时,您必须返回数组中的对象数。
Then you have to return the number of objects in the array when numberOfRowsInSection gets called.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [arr count];
}
您的cellForRowAtIndexPath委托方法应如下所示以打印出数组中的字符串
Your cellForRowAtIndexPath delegate method should look like this to print out the strings in the array.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Formal";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.textLabel.text = [arr objectAtIndex:indexPath.row];
return cell;
}
希望这会有所帮助!
这篇关于在XCode中将单元格添加到表视图(UITableView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!