2

We are using - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; method to retrieve a cell at specific index path. - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath will return an object representing a cell of the table or nil if the cell is not visible or indexPath is out of range. However repeating calls to - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath causes NSRange exception. What can be the reason?

NSIndexPath *ip = [NSIndexPath indexPathForRow:index inSection:0];
        CustomCell *cell = (CustomCell *)[tableview_ cellForRowAtIndexPath:ip];

code

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = nil;
static NSString *cellIdentifier = @"Cell";
cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
    NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"customcell" owner:self options:nil];
    cell = [nibs objectAtIndex:0];
}
ImageCache *imageCache = [[ImageCache alloc]initWithUrl:sentCard.sentImage];
    UIImage *cachedImage = [imageCache fetchImageFromCache];
    if (cachedImage!=nil) {
        cell.cardImage.image = [Utility scaleImageToSize:cell.cardImage.frame.size image:cachedImage];
        [cell.loadingIndicator stopAnimating];
    }
    else {
        imageCache.cacheDelegate = self;
        imageCache.cacheDuration = 24.0;
        [imageCache fetchImage];
    }
return cell;
}
Neo
  • 936
  • 6
  • 17
  • see this link http://stackoverflow.com/questions/8239666/nsrangeexception-call-stack-not-showing-line-number – Baby Groot Jul 19 '13 at 10:34
  • Would you show your `cellForRowATIndexPath:` method? – Mani Jul 19 '13 at 10:41
  • you are not returning any cell.. ;) its a typo, not reason of your error though – egghese Jul 19 '13 at 10:47
  • @Jesly Varghese Sorry, It was a mistake in editing post. – Neo Jul 19 '13 at 10:50
  • please post the complete error message. – Ahmed Mohammed Jul 19 '13 at 10:56
  • Its probably an array causing the crash, and its no where in the code you have provided here... Please give the context for `NSIndexPath *ip = [NSIndexPath indexPathForRow:index inSection:0]; CustomCell *cell = (CustomCell *)[tableview_ cellForRowAtIndexPath:ip];` – egghese Jul 19 '13 at 10:59

1 Answers1

0

You are asking for a cell that is not being created yet. For example, that should crash on the first cell (index 0). You should ask for a cell (aka cellForRowAtIndexPath) once the table as being populated.

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • UITableView documentation specifies that - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath will return an object representing a cell of the table or nil if the cell is not visible or indexPath is out of range. So if there is no cell at the specified index path, it will return nil. However here call to (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath produce NSRangeException. – Neo Jul 19 '13 at 10:41
  • What are you returning on `numberOfRowsInSection`? – Rui Peres Jul 19 '13 at 10:43
  • It won't cause a crash/range exception, gracefully returns nil – egghese Jul 19 '13 at 10:52
  • Where is the location of the crash? – Rui Peres Jul 19 '13 at 10:55