【问题标题】:Adding cells to table view (UITableView) in XCode在 XCode 中将单元格添加到表视图 (UITableView)
【发布时间】:2012-06-07 16:05:54
【问题描述】:

我是 X-Code 的新手,刚刚开始开发我的第一个应用程序。我正在使用故事板。在 Navigation Controller 场景中,我添加了一个包含两个单元格的 MasterViewController,这导致两个 DetailViewControllers(Detail1 和 Detail 2)。

  • 列数 = 1
  • 列中的行数 = 2

我在属性检查器中为单元格添加了重用指示器,并在情节提要中建立了连接。

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

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

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

有人可以帮我理解如何在代码中添加更多单元格,或许可以多理解一下表格视图代码吗?

这是到目前为止表格视图的代码:

#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

【问题讨论】:

    标签: xcode uitableview cell


    【解决方案1】:

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

    然后你必须在 numberOfRowsInSection 被调用时返回数组中对象的数量。

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

    你的 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;
    }
    

    希望这会有所帮助!

    【讨论】:

    • 这对我帮助很大,我仍然花了一些时间来弄清楚这一切,但现在我明白了。 :)
    • @KristofferBilek 如果对您有帮助,请考虑支持我的回答。谢谢!
    • 对不起,我也是这个地方的新手。我试过了,但我没有足够的声望点来做这件事。也许我应该将新信息添加到您的答案中,而不是将其作为新答案发布。嗯
    • @KristofferBilek 别担心。 NP
    【解决方案2】:

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

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

    在 .h 中有这个

    @interface OmMasterViewController : UITableViewController {
    
    NSMutableArray *omMeny;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 1970-01-01
      • 2019-04-18
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      相关资源
      最近更新 更多