Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In my refresh method I'm using the following code to scroll to top which is working fine, but when I scroll down after the third cell and hit refresh again it crashes the app with error terminating with uncaught exception of type NSException.

[TableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];

The UITableView has 7 dynamic cells. Do I need to add something to make it work for the whole tableview?

I searched on StackOverflow for a solution, but none of them solved my problem unfortunately.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
174 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your cellForRowAtIndexPath function tries to access an array object that it's beyond its bounds as the error says. I am guessing you have an array that is being updated with different objects and the size is not always the same or if it is the array is not always full of objects.

What I would suggest before doing something like this:

cell.myCellTitle.text = [yourArrayVariable objectAtIndex:indexPath.row];

to accessing your data, check first the count of the array is greater than your indexPath.row that cellForRowAtIndexPath is trying to call.

if (yourArrayVariable.count > indexPath.row) {
cell.myCellTitle.text = [yourArrayVariable objectAtIndex:indexPath.row]
}

This way whenever you try to reload your tableView you will be sure at least the function won't go beyond the NSArray bounds.

And one more thing, you should be careful about when and where you are trying to define your array and how you are initializing it. If you reload the tableView every time that you hit refresh, then you are probably assigning some new data to the array and you should consider reloading after you are properly initialized the array with your data.

Perhaps some snippets would help to see what you do in viewDidLoad, your refresh action, and cellForRowAtIndexPath delegate. It will also help to see the nature of your app and what you are trying to do in that UITableView controller. Things like getting the data from remote server and using dispatch_queue_t and call your reload from dispatch_async(dispatch_get_main_queue().

Hit me in comment if you need more details and I will update this answer as you develop your question.

Have happy coding:)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...