简体   繁体   中英

Getting nill UITableviewcell from indexpath

I am getting nil UITableviewcell,check my below code.

  int random = arc4random() % arr.count;                                     
  NSInteger index = [[arr objectAtIndex:random-1] integerValue];
  NSIndexPath *path = [NSIndexPath indexPathForRow:index inSection:0];
  SongListCell *cell2 = (SongListCell *)[table_view cellForRowAtIndexPath:path];
  • Getting nill cell.

what is wrong? what i am doing wrong?

方法cellForRowAtIndexPath:如果单元格不可见或索引路径超出范围,则返回nil

I'm not sure exactly what you're trying to achieve, but I think that maybe the second row of your code shouldn't be there. Is that possible? Shouldn't it be:

  int random = arc4random() % arr.count;                                     
  NSIndexPath *path = [NSIndexPath indexPathForRow:random-1 inSection:0];
  SongListCell *cell2 = (SongListCell *)[table_view cellForRowAtIndexPath:path];

Because otherwise, your depending on the values inside arr and not just on the amount of objects in arr , and these values may contain invalid indices (or maybe even non numeric ones).

If that's not the case, can you please provide an example of values from arr ?

You should create the cell yourself if it is nil, alternatively in your viewDidLoad or where appropriate, you can call one of these methods to make the tableview create the cells for you.

[self.tableView registerClass:<#(__unsafe_unretained Class)#> forCellReuseIdentifier:<#(NSString *)#>]
[self.tableView registerNib:<#(UINib *)#> forCellReuseIdentifier:<#(NSString *)#>]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM