我有一个UITableView
从NSMutableArray
名为friendsList的项目中读取项目。
我在这里初始化这个数组:
- (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
方法。numberOfRows
friendslist.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;
}
问题是在此表保持空白后,数组中的项目不会显示。如果我用手指触摸桌子,项目会立即出现,就好像触摸以某种方式刷新了桌子。有什么想法可能导致这种情况吗?
我正在使用 iOS 7 的 iPhone 5 上测试这个,从 XCode 5.0.1 构建。