2017-10-21 229 views
0

我一直在爲此奮鬥了大約一個小時,不明白爲什麼我的單元格沒有顯示在我的tableview中。我按照教程做了他提到要做的事情,但仍然有問題。我已將重用標識符設置爲main,我的委託和數據源都設置爲self。我在控制檯中沒有收到任何錯誤,並且我的數據通過我的服務器傳出。我打印出標題,描述等等,它就在那裏。幫助單元格顯示爲空

#import "HTTPService.h" 
#import "ViewController.h" 
#import "VideoCell.h" 
#import "Video.h" 

@interface ViewController() 
@property (weak, nonatomic) IBOutlet UITableView *tableView; 
@property (nonatomic,strong) NSArray *videoList; 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.tableView.dataSource = self; 
    self.tableView.delegate = self; 

    self.videoList = [[NSArray alloc]init]; 

    [[HTTPService instance]getTutorials:^(NSArray * _Nullable 
dataArray, NSString * _Nullable errMessage) 
    { 
     if(dataArray){ 
      NSMutableArray *arr = [[NSMutableArray alloc]init]; 

      for (NSDictionary *d in dataArray){ 
       Video *vid = [[Video alloc]init]; 
       vid.videoTitle = [d objectForKey:@"title"]; 
       vid.videoDescription = [d objectForKey:@"description"]; 
       vid.thumbnailURL = [d objectForKey:@"thumbnail"]; 
       vid.videoIFrame = [d objectForKey:@"iframe"]; 

       [arr addObject:vid]; 
      } 

      self.videoList = arr; 
      [self updateTableData]; 

     } 
     else if(errMessage) 
      NSLog(@"Alert to user: %@",errMessage); 
    }]; 


} 

-(void) updateTableData{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.tableView reloadData]; 
    }); 
} 

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

    VideoCell *vCell = (VideoCell*)[tableView 
dequeueReusableCellWithIdentifier:@"main"]; 

    if(!vCell) 
     vCell = [[VideoCell alloc]init]; 
    NSLog(@"here heree"); 
    return vCell; 
} 

- (NSInteger)tableView:(nonnull UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"hererrrr"); 
    return self.videoList.count; 
} 

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath: 
(nonnull NSIndexPath *)indexPath{ 

} 

-(void)tableView:(UITableView*)tableView willDisplayCell:(nonnull 
UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath 
*)indexPath{ 
    NSLog(@"Im in this bitch"); 
    Video *vid = [self.videoList objectAtIndex:indexPath.row]; 
    VideoCell *vidCell = (VideoCell*)cell; 
    [vidCell updateUI:vid]; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 

@end 

讓我知道你是否需要看到任何導入文件!

enter image description here

enter image description here

enter image description here

+0

您是否在故事板或NIB中連接了'tableView'插座? –

+0

地道的事情是在'cellForRow:'而不是'willDisplayCell'中配置你的單元格。 在'willDisplayCell:'中放置一個斷點,是否在那裏調用?你是否將故事板連接到插座? – dannyzlo

+0

@MatusalemMarques我相信如此!檢查我剛添加的圖像。 – humbleCoder

回答

0

你的代碼是正確的,除了一兩件事。你忘了將你的單元格註冊到你的tableView。只要把你的viewDidLoad

[self.tableView registerNib:[UINib nibWithNibName:@"VideoCell" bundle:nil] forCellReuseIdentifier:@"main"]; 

一切都會好的。 這是我們都做的一個常見錯誤。