Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

ios - Scroll to top after refreshing UITableView crashes after third cell

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

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

1 Reply

0 votes
by (71.8m points)

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:)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...