0

this is the crash I am receiving right now. Can someone please help me figure out how to resolve this? It occurs on this line UITableViewCell* cell=[_tableView cellForRowAtIndexPath:[_tableView indexPathForRowAtPoint:point]]; I thought if I check if indexPath was valid it would resolve it, but it didn't. Any way to stop this from occurring? Any tips or suggestions are appreciated. I will post what I tried to do below.

NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:point];
if (indexPath) {
UITableViewCell* cell=[_tableView cellForRowAtIndexPath:[_tableView    indexPathForRowAtPoint:point]];
.... rest of my code
  }

3 Answers 3

2

You might reload the table view before initialising array , or may be your array is empty when you use it !

Like when you use :

NSString *xyz = [self.your_array objectAtIndex:indexPath.row]; 

in cellForRowAtIndexPath , Try fllowing code that wont crash your app !

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

static NSString *CellIdentifier = @"xyz_cell";
long nodeCount = [self.your_array count];

if (nodeCount == 0 && indexPath.row == 0)
{
    Home_CellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[Home_CellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier:CellIdentifier] ;
        cell.lbl.textAlignment = NSTextAlignmentCenter;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.lbl.text = @"No Posts yet !";

    return cell;
}


else{

Home_CellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (cell == nil) {
    cell = [[Home_CellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
} // Your Code Goes HERE .... NSString *xyz = [self.your_array objectAtIndex:indexPath.row]; }

so , for empty array it wont find value for key one and your app wont crash !

I hope it helps ,

Sign up to request clarification or add additional context in comments.

Comments

0

Your tableView doesn't have more than a single row I suspect (or none?).

As your error message indicates, you're trying to reference object at index 1, but the bounds of the array are 0 .. 0.

If you're executing this before the tableView is initialized you'll need to change where you execute it, or perhaps you forgot to implement the tableView:numberOfRowsInSection: delegate method?

Comments

0

As you told in your question that error coming in this line, it means that indexPath is holding some reference Try change this line

if (indexPath) {
    UITableViewCell* cell=[_tableView cellForRowAtIndexPath:[_tableView  indexPathForRowAtPoint:point]];
}

to

if (indexPath) {
        UITableViewCell* cell=[_tableView cellForRowAtIndexPath:indexPath]; 
    }

But, i suspect that you are not having more than 1 item in your tableview.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.