1

と呼ばれるfriendsListUITableViewからアイテムを読み取るがあります。NSMutableArray

ここでこの配列を初期化します。

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.title = TA(@"Find Friends", @"");
        self.navigationItem.rightBarButtonItem = [self createRightNavBarButton];
        self.navigationItem.leftBarButtonItem = [self createLeftNavBarButton];
        friendsList = [[NSMutableArray alloc]init];
    }
    return self;
}

カウントがゼロより大きい場合に返すdatasourceメソッドを設定しました。numberOfRowsfriendslist.count

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (friendsList.count) {
        return friendsList.count;
    }
    else{
        return 0;
    }

}

呼び出している別のメソッドで配列を埋めた後[tableView reloadData]datasourceメソッドが再度呼び出されて配列内のオブジェクトの数が読み取られるようにします。

- (void)loadContactsFromSource:(ListSource)source
{
    if (friendsList) {
        [friendsList removeAllObjects];
    }
    switch (source) {
        case LS_Facebook:
            break;
        case LS_Twitter:            
            break;
        case LS_Contacts:{
            ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(nil, nil);
            ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
                if (granted) {
                    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
                    CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(
                                                                               kCFAllocatorDefault,
                                                                               CFArrayGetCount(allPeople),
                                                                               allPeople
                                                                               );
                    CFArraySortValues(
                                      peopleMutable,
                                      CFRangeMake(0, CFArrayGetCount(peopleMutable)),
                                      (CFComparatorFunction) ABPersonComparePeopleByName,
                                      (void*) ABPersonGetSortOrdering()
                                      );

                    CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

                    for ( int i = 0; i < nPeople; i++ )
                    {
                        ABRecordRef ref = CFArrayGetValueAtIndex(peopleMutable, i);
                        [self copyContactToArray:ref];
                    }
                    [tableFriends reloadData];
                }
                else{
                    [PopupHandler popupDialogWithTitle:T(@"Error", @"")
                                               message:T(@"Access denied", @"")
                                              delegate:nil];
                }
            });
            break;
        }
        default:
            break;
    }
}

cellForRow メソッドは次のとおりです。

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

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    NSDictionary *currentPerson = [friendsList objectAtIndex:indexPath.row];
    cell = [self fillCell:cell withUserInfo:currentPerson];

    return cell;
}

問題は、このテーブルが空白のままになった後、配列の項目が表示されないことです。指でテーブルに触れると、テーブルがリフレッシュされたかのように、アイテムがすぐに表示されます。これを引き起こしている可能性のあるアイデアはありますか?

XCode 5.0.1 から構築された iOS 7 を搭載した iPhone 5 でこれをテストしています。

4

1 に答える 1

0

別のスレッドで呼び出されているように見えABAddressBookRequestAccessWithCompletion、完了時に実行されるブロックはメインスレッドにありません。そのため、ブロック内の UI を更新しても正しく機能しません。関数を作成し、アドレスブック リターン内からメイン スレッドでその関数を呼び出すか、アドレスブック リターン ブロック内で dispatch_async を実行してみてください。

またはを使用して、リターン ブロック内でABAddressBookRequestAccessWithCompletionディスパッチからメイン スレッドに移動します。dispatch_asyncperformSelectorOnMainThread

于 2013-11-11T16:40:02.197 に答える