【问题标题】:How to populate a table view with an NSMutableArray如何使用 NSMutableArray 填充表视图
【发布时间】:2015-11-11 02:17:10
【问题描述】:

我复制了示例代码以在我的应用程序中创建一个 Objective-C 表 (http://www.appcoda.com/uitableview-tutorial-storyboard-xcode5/)。该示例效果很好。

我遇到的问题是该示例使用了预定义的项目数组,但我的应用在加载视图控制器时生成了数组项目列表。

我的应用正在生成一个电影学分列表:

NSMutableArray *creditList;

在 viewDidLoad 我有:

creditList = [NSMutableArray arrayWithObjects:@"Test Movie 1", nil];

然后我生成电影列表。循环完成后,列表将正确存储在数组 creditList 中,具体取决于:

NSLog(@"creditList array: %@", creditList);

我现在如何使用生成的项目列表填充表格,我将把代码放在哪里?

提前致谢, 李

【问题讨论】:

    标签: ios objective-c arrays uitableview nsmutablearray


    【解决方案1】:

    下面是步骤

    1. 首先你必须有tableview和一个可重复使用的标识符,在这种情况下我只是把它叫做“cell”
    2. 由于您已经拥有数组和数组中的值,因此您现在必须设置部分和行,正如您在此代码中看到的那样

      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
           return 1;    
         } 
      
       - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
             return creditList.count; 
         }
      
    3. 现在您必须使用此代码设置实际的单元格

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
      
           cell.textLabel.text = [[creditList objectAtIndex:indexPath.row] capitalizedString];
      
           return cell;
        }
      

    【讨论】:

    • 感谢您的步骤。由于它们与我提供的示例链接中的步骤基本相同,因此我得到了相同的结果(您在步骤 4 中显示),其中仅显示了数组的初始内容,而不是我使用时看到的完整数组: NSLog(@"creditList 数组:%@", creditList);
    • 嗨@lee 所以基本上你是从网络服务获取电影的吧?
    • 是的,Cesar,它们是使用 TMDb API 提取的,并作为 JSON 响应返回。这是填充数组的代码: while (myCount
    • 所以首先在 viewdidload 中放 [[NSMutable Array alloc]init];然后在方法中填充数组放入 [self.tableView reloadData];
    • 我添加了 [[NSMutableArray alloc]init];进入 viewDidLoad,我收到了“未使用表达式结果”的软警告。我还添加了“[self.tableView reloadData];”在我发布显示正确 creditList 的 NSLOG 之后。当我运行该应用程序时,它仍然只显示 creditList 的原始内容('Test Movie 1)。塞萨尔,感谢您为提供帮助所付出的努力。
    【解决方案2】:

    您可以使用NSMutableArrayNSArray 相同。无需再做任何事情。

    物品生成后。你只需要打电话:

    [tableView reloadData];

    【讨论】:

    • 感谢您的回复。当我在 NSLOG 语句之后将该代码放在 viewDidLoad 中时,我得到“未知接收器'tableview';你的意思是'UITableView'吗?”如果我将其更改为 UITableView,那么我会得到“No known class method for selector 'reloadData'”。
    • tableViewUITableView 的处理程序。
    • [tableView reloadData]; .. 有帮助
    【解决方案3】:

    要使用您的数据填充表格,常用的设计模式是 UITableViewDelegate,在您的实现 (.m) 文件中添加委托,如下所示:

    @interface ViewController () <UITableViewDelegate, UITableViewDataSource>
    

    然后在同一个ViewController.m文件中调用两个函数,第一个是:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return [_creditList count];
      }
    

    第二个功能:

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
              static NSString *cellName = @"cellNamedInStoryBoard";
    
           UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    
           if (cell == nil) {
           cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
           } 
    
          cell.textLabel.text = [_creditList objectAtIndex:indexPath.row];
          NSLog(@"cell label created");
    
          return cell;
       }
    

    您需要在情节提要中设置数据源和委托,您可以通过右键单击表格视图并将其拖动到黄色的 ViewController 图标并选择委托和数据源来完成。

    完成此操作后,如果您的数组填充了字符串,您应该会在运行应用程序时看到表格视图中列出的项目。

    【讨论】:

    • 感谢您的全面回复。您详细介绍的所有内容几乎都是我所拥有的,并且在我提供的我使用的示例的链接中。不幸的是,由于这个原因,我仍然在桌面上显示“测试电影 1”。
    猜你喜欢
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    相关资源
    最近更新 更多