在 XCode 中将单元格添加到表格视图(UITableView)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10866813/
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 00:28:27  来源:igfitidea点击:

Adding cells to table view (UITableView) in XCode

xcodeuitableviewcell

提问by ingenspor

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).

我是 X-Code 的新手,刚刚开始开发我的第一个应用程序。我正在使用故事板。在导航控制器场景中,我添加了一个带有两个单元格的 MasterViewController,这导致了两个 DetailViewController(Detail1 和 Detail 2)。

  • Number of Columns = 1
  • Number of rows in columns = 2
  • 列数 = 1
  • 列中的行数 = 2

I've added a Reuse Indicator for the cells in the attributes inspector, and made the connection in the storyboard.

我在属性检查器中为单元格添加了一个重用指示器,并在故事板中建立了连接。

But when I run the app, I can only see the first cell x2.

但是当我运行应用程序时,我只能看到第一个单元格 x2。

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!

当我查看 MasterViewController.m 时,我可以看到第一个单元格的代码,但我不明白如何插入第二个单元格等!

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,,

我知道解决方案很简单,我已经寻找了 3 天的答案,我觉得自己很愚蠢,但我现在真的需要一些帮助,

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

采纳答案by Alexander of Norway

Lets say you have an array called arr.

假设您有一个名为 arr 的数组。

Then you have to return the number of objects in the array when numberOfRowsInSection gets called.

然后,当 numberOfRowsInSection 被调用时,您必须返回数组中的对象数。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [arr count];
}

Your cellForRowAtIndexPath delegate method should look like this to print out the strings in the array.

您的 cellForRowAtIndexPath 委托方法应如下所示以打印出数组中的字符串。

- (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;
}

Hope this helps!

希望这可以帮助!

回答by ingenspor

My code now looks like this:

我的代码现在看起来像这样:

- (void)viewDidLoad
{
[super viewDidLoad];

omMeny = [[NSMutableArray alloc] init];

//Add items
[omMeny addObject:@"Form?let med guiden"];
[omMeny addObject:@"Aromahjulet"];
[omMeny addObject:@"Fagterm"];


//Set the title
self.navigationItem.title = @"Om R?dvin";
}

//dealloc method declared in RootViewController.m
- (void)dealloc {

[omMeny release];
[super dealloc];
}


// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#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;
return [omMeny count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Om";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Set up the cell...
NSString *cellValue = [omMeny objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

// Configure the cell...

return cell;
}

#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

with this in the .h

在 .h 中有这个

@interface OmMasterViewController : UITableViewController {

NSMutableArray *omMeny;
}