【问题标题】:Array not showing in Table View数组未显示在表视图中
【发布时间】:2013-07-07 17:04:37
【问题描述】:

谁能告诉我为什么我的代码没有在我的表格视图中显示任何结果。这是我的代码。我已经尝试将 @"@" 更改为 indexPath.row 没有任何运气。我正在寻找正确方向的任何答案。我对xcode和objective-c还很陌生。我真的很感激任何帮助。

-(void)waitAndFillPlaylistPool {
    // arrPlaylist -> mutablearray which stores the value of loaded playlist in order to use it later


    [SPAsyncLoading waitUntilLoaded:[SPSession sharedSession] timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *loadedession, NSArray *notLoadedSession)
     {
         // The session is logged in and loaded — now wait for the userPlaylists to load.
         NSLog(@"[%@ %@]: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), @"Session loaded.");

         [SPAsyncLoading waitUntilLoaded:[SPSession sharedSession].userPlaylists timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *loadedContainers, NSArray *notLoadedContainers)
          {
              // User playlists are loaded — wait for playlists to load their metadata.
              NSLog(@"[%@ %@]: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), @"Container loaded.");

              NSMutableArray *playlists = [NSMutableArray array];
              [playlists addObject:[SPSession sharedSession].starredPlaylist];
              [playlists addObject:[SPSession sharedSession].inboxPlaylist];
              [playlists addObjectsFromArray:[SPSession sharedSession].userPlaylists.flattenedPlaylists];

              [SPAsyncLoading waitUntilLoaded:playlists timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *loadedPlaylists, NSArray *notLoadedPlaylists)
               {
                   // All of our playlists have loaded their metadata — wait for all tracks to load their metadata.
                   NSLog(@"[%@ %@]: %@ of %@ playlists loaded.", NSStringFromClass([self class]), NSStringFromSelector(_cmd),
                         [NSNumber numberWithInteger:loadedPlaylists.count], [NSNumber numberWithInteger:loadedPlaylists.count + notLoadedPlaylists.count]);
                   NSLog(@"loadedPlaylists >> %@",loadedPlaylists);

                   arrPlaylist = [[NSMutableArray alloc] initWithArray:loadedPlaylists];
                   NSLog(@"arrPlaylist >> %@",arrPlaylist);

               }];
          }];
     }];
}


- (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section {

    return [arrPlaylist count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

        cell.textLabel.text = [arrPlaylist objectAtIndex:indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    return cell;
}

【问题讨论】:

  • 你确定你的代码运行成功,没有崩溃 [arrPlaylist objectAtIndex:@"%@"];这一行?
  • 数组数据全部下载后重新加载UITableView的代码在哪里?你确定你正确设置了 UITableViewDelegate 和 UITableViewDataSource 吗?
  • 您在以下代码行中错误地引用了数组的值 cell.textLabel.text = [arrPlaylist objectAtIndex:@"%@"] 因为您没有指定应该从哪个索引中获取值数组,应该是 cell.textLabel.text = [arrPlaylist objectAtIndex:indexPath.row]。
  • 同意@iAmbitious的评论!!!
  • 我更改它没有任何运气...我的表格视图仍然没有显示任何数据。

标签: iphone objective-c nsmutablearray cocoalibspotify-2.0 libspotify


【解决方案1】:

很难说你在方法waitAndFillPlaylistPool中做了什么,但如果这需要任何时间来获取这些数据,那么你需要在你的表视图([self.tableView reloadData])上调用reloadData作为该方法的最后一行(或者当任何异步方法返回时——我不知道它可能在你的代码中的什么位置)。

【讨论】:

  • 太棒了...我确实在我的方法结束时创建了一个 reloadData。然后,我创建了我的 tableview naam 的属性出口,并且确实有效....检索名称时出现问题,但我设法通过引用名称 NSString 来更改代码。非常感谢你们,这是我第一次使用 stackoverflow,你们的帮助很棒。你们所有人......谢谢......我仍然想知道为什么需要使用 reloadData。
  • @DanhiBus,表视图在视图控制器的 viewDidLoad 方法运行之后(但在 viewWillAppear 之前)立即调用它的方法,所以如果你有一个异步进程来获取表所需的数据,表将已经调用 numberOfRows (如果您的数组尚未填充,则为 0)。因此,您需要在异步过程之后调用 reloadData,这会导致表视图再次调用其数据源方法。如果我的回答有助于解决您的问题,您应该点击箭头接受它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-10
  • 1970-01-01
相关资源
最近更新 更多