无法在 iOS 应用程序中重新加载 TableView 数据

发布于 2024-12-19 13:58:49 字数 4857 浏览 1 评论 0 原文

我发现自己没有办法让这个应用程序运行起来。我的应用程序使用拆分视图来显示列表。 “主”列表应包含用户的 Facebook 好友列表。由于该应用程序不会强制您登录,因此如果您尚未登录,它会在您登录之前在列表中显示“您没有朋友”。我的问题是,一旦我加载了所有朋友并致电[self.tableView reloadData] 我的程序在那里崩溃了一些,尽管我尽了最大努力调试它,但我找不到它。方法是

    - (void)request:(FBRequest *)request didLoad:(id)result
    {
        if ([result isKindOfClass:[NSArray class]] && ([result count] > 0)) {
            result = [result objectAtIndex:0];
        }
        switch (((Facebook *)[Facebook shared]).currentCall) {
            case graphUserFriends:
            {
                _friends = [NSMutableArray array];
                NSArray *resultData = [result objectForKey:@"data"];
                if ([resultData count] > 0) 
                {
                    for (NSUInteger i=0; i<[resultData count] && i < 25; i++) 
                    {
                        NSDictionary *friendDictionary = [resultData objectAtIndex:i];
                        FbFriend * f = [[[FbFriend alloc] initWithName:[friendDictionary objectForKey:@"name"] Id:[friendDictionary objectForKey:@"id"]] autorelease];
                        [_friends addObject:f];
                    }
                }
                [self.tableView reloadData];
                break;
            }
            default:
                break;
        }
    }

整个源代码(和Xcode 4项目)可以从https://skydrive.live.com/redir.aspx?cid=04b38cdd7b38bb7f&resid=4B38CDD7B38BB7F!798&parid=4B38CDD7B38BB7F!470&authkey=!API4iVva95nZFL8

控制台(崩溃时):

GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug 15 16:03:10 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 48870.
Catchpoint 3 (throw)Pending breakpoint 1 - "objc_exception_throw" resolved
Current language:  auto; currently objective-c
(gdb) bt
#0  0x0156ecf0 in objc_exception_throw ()
#1  0x013c9674 in -[__NSArrayI objectAtIndex:] ()
#2  0x00454805 in -[UITableViewDataSource tableView:heightForRowAtIndexPath:] ()
#3  0x0026427a in -[UITableViewController tableView:heightForRowAtIndexPath:] ()
#4  0x0020f548 in -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] ()
#5  0x00211722 in -[UITableViewRowData numberOfRows] ()
#6  0x000c17c7 in -[UITableView noteNumberOfRowsChanged] ()
#7  0x000c12c1 in -[UITableView reloadData] ()
#8  0x0000247e in -[MasterViewController request:didLoad:] (self=0x6a491e0, _cmd=0x12e65, request=0x681e930, result=0x6824250) at /Users/CheckM8/Documents/Xcode 4/Projects/iPeople4/iPeople4/MasterViewController.m:46
#9  0x00009d36 in -[FBRequest handleResponseData:] (self=0x681e930, _cmd=0x1397a, data=0x6a76c60) at /Users/CheckM8/Documents/Xcode 4/facebook-facebook-ios-sdk-74358cd/src/FBRequest.m:261
#10 0x0000a357 in -[FBRequest connectionDidFinishLoading:] (self=0x681e930, _cmd=0xade62e, connection=0x681ec40) at /Users/CheckM8/Documents/Xcode 4/facebook-facebook-ios-sdk-74358cd/src/FBRequest.m:346
#11 0x00a29a59 in ___NSURLConnectionDidFinishLoading_block_invoke_0 ()
#12 0x00a27e94 in __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke_0 ()
#13 0x00a28eb7 in -[NSURLConnectionInternalConnection invokeForDelegate:] ()
#14 0x00a27e4f in -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] ()
#15 0x00a27fd5 in -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] ()
#16 0x0096cf6a in _NSURLConnectionDidFinishLoading ()
#17 0x0398fbbd in URLConnectionClient::_clientDidFinishLoading ()
#18 0x03a5c5ea in URLConnectionClient::ClientConnectionEventQueue::processAllEventsAndConsumePayload ()
#19 0x03986298 in URLConnectionClient::processEvents ()
#20 0x03a5c16b in non-virtual thunk to URLConnectionInstanceData::multiplexerClientPerform() ()
#21 0x03986137 in MultiplexerSource::perform ()
#22 0x013b197f in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#23 0x01314b73 in __CFRunLoopDoSources0 ()
#24 0x01314454 in __CFRunLoopRun ()
#25 0x01313db4 in CFRunLoopRunSpecific ()
#26 0x01313ccb in CFRunLoopRunInMode ()
#27 0x012c6879 in GSEventRunModal ()
#28 0x012c693e in GSEventRun ()
#29 0x00034a9b in UIApplicationMain ()
#30 0x00001d82 in main (argc=1, argv=0xbfffed64) at /Users/CheckM8/Documents/Xcode 4/Projects/iPeople4/iPeople4/main.m:16
#31 0x00001cf5 in start ()

I find my self out of ideas trying to get this app to work. My app uses a split view to show to lists. the "Master" list should hold a list of the user's Facebook friends. Since the app does not force you to login, if you're not logged in yet it shows "You have no friends" in the list till you've logged in. My problem is that once I've loaded all the friends and call [self.tableView reloadData] my program crashes some in there, and despite my best attempts at debugging it I can't find it. The method is

    - (void)request:(FBRequest *)request didLoad:(id)result
    {
        if ([result isKindOfClass:[NSArray class]] && ([result count] > 0)) {
            result = [result objectAtIndex:0];
        }
        switch (((Facebook *)[Facebook shared]).currentCall) {
            case graphUserFriends:
            {
                _friends = [NSMutableArray array];
                NSArray *resultData = [result objectForKey:@"data"];
                if ([resultData count] > 0) 
                {
                    for (NSUInteger i=0; i<[resultData count] && i < 25; i++) 
                    {
                        NSDictionary *friendDictionary = [resultData objectAtIndex:i];
                        FbFriend * f = [[[FbFriend alloc] initWithName:[friendDictionary objectForKey:@"name"] Id:[friendDictionary objectForKey:@"id"]] autorelease];
                        [_friends addObject:f];
                    }
                }
                [self.tableView reloadData];
                break;
            }
            default:
                break;
        }
    }

The whole source code (and Xcode 4 project) can be downloaded from https://skydrive.live.com/redir.aspx?cid=04b38cdd7b38bb7f&resid=4B38CDD7B38BB7F!798&parid=4B38CDD7B38BB7F!470&authkey=!API4iVva95nZFL8

Console (At Crash):

GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug 15 16:03:10 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 48870.
Catchpoint 3 (throw)Pending breakpoint 1 - "objc_exception_throw" resolved
Current language:  auto; currently objective-c
(gdb) bt
#0  0x0156ecf0 in objc_exception_throw ()
#1  0x013c9674 in -[__NSArrayI objectAtIndex:] ()
#2  0x00454805 in -[UITableViewDataSource tableView:heightForRowAtIndexPath:] ()
#3  0x0026427a in -[UITableViewController tableView:heightForRowAtIndexPath:] ()
#4  0x0020f548 in -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] ()
#5  0x00211722 in -[UITableViewRowData numberOfRows] ()
#6  0x000c17c7 in -[UITableView noteNumberOfRowsChanged] ()
#7  0x000c12c1 in -[UITableView reloadData] ()
#8  0x0000247e in -[MasterViewController request:didLoad:] (self=0x6a491e0, _cmd=0x12e65, request=0x681e930, result=0x6824250) at /Users/CheckM8/Documents/Xcode 4/Projects/iPeople4/iPeople4/MasterViewController.m:46
#9  0x00009d36 in -[FBRequest handleResponseData:] (self=0x681e930, _cmd=0x1397a, data=0x6a76c60) at /Users/CheckM8/Documents/Xcode 4/facebook-facebook-ios-sdk-74358cd/src/FBRequest.m:261
#10 0x0000a357 in -[FBRequest connectionDidFinishLoading:] (self=0x681e930, _cmd=0xade62e, connection=0x681ec40) at /Users/CheckM8/Documents/Xcode 4/facebook-facebook-ios-sdk-74358cd/src/FBRequest.m:346
#11 0x00a29a59 in ___NSURLConnectionDidFinishLoading_block_invoke_0 ()
#12 0x00a27e94 in __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke_0 ()
#13 0x00a28eb7 in -[NSURLConnectionInternalConnection invokeForDelegate:] ()
#14 0x00a27e4f in -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] ()
#15 0x00a27fd5 in -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] ()
#16 0x0096cf6a in _NSURLConnectionDidFinishLoading ()
#17 0x0398fbbd in URLConnectionClient::_clientDidFinishLoading ()
#18 0x03a5c5ea in URLConnectionClient::ClientConnectionEventQueue::processAllEventsAndConsumePayload ()
#19 0x03986298 in URLConnectionClient::processEvents ()
#20 0x03a5c16b in non-virtual thunk to URLConnectionInstanceData::multiplexerClientPerform() ()
#21 0x03986137 in MultiplexerSource::perform ()
#22 0x013b197f in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#23 0x01314b73 in __CFRunLoopDoSources0 ()
#24 0x01314454 in __CFRunLoopRun ()
#25 0x01313db4 in CFRunLoopRunSpecific ()
#26 0x01313ccb in CFRunLoopRunInMode ()
#27 0x012c6879 in GSEventRunModal ()
#28 0x012c693e in GSEventRun ()
#29 0x00034a9b in UIApplicationMain ()
#30 0x00001d82 in main (argc=1, argv=0xbfffed64) at /Users/CheckM8/Documents/Xcode 4/Projects/iPeople4/iPeople4/main.m:16
#31 0x00001cf5 in start ()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不顾 2024-12-26 13:58:49

你犯了一个典型的内存管理错误:你直接访问你的ivars并且它烧毁了你。

_friends = [NSMutableArray array];

这是保留不足(稍后会崩溃),如果 _friends 有先前的值,也可能发生泄漏。除了在 dealloc 和 init 中之外,您应该始终对 ivars 使用访问器:

self.friends = [NSMutableArray array];
...
[self.friends addObject:f];

编辑:从您的堆栈跟踪中,您的错误位于表视图数据源的 tableView:heightForRowAtIndexPath: 中。看起来您正在读取数组的末尾。

You've made the classic memory management mistake: You're directly accessing your ivars and it burned you.

_friends = [NSMutableArray array];

This is an under-retain (which will crash later), and also a possible leak if _friends had a prior value. You should always use accessors for your ivars except in dealloc and init:

self.friends = [NSMutableArray array];
...
[self.friends addObject:f];

EDIT: From your stacktrace, your bug is in your table view data source's tableView:heightForRowAtIndexPath:. It looks like you're reading off the end of your array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文