2017-10-21 234 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"]; 

一切都会好的。 这是我们都做的一个常见错误。