选择ABPeoplePicker后重新加载UITableView

时间:2014-09-17 15:25:51

标签: ios objective-c uitableview abpeoplepickerview

所以,我在UITableView中加载了UIViewController。 该表从NSMutableArray

获取其数据

我在UIBarButtonItem上添加了NavigationBar,会打开ABPeoplePickerNavigationController来选择联系人,并在NSMutableArray中添加其名称以显示在UITableView中1}}

这是我的代码摘要

@interface LockedChatsTableView : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, ABPeoplePickerNavigationControllerDelegate>
@property (strong, nonatomic, retain) UITableView *tabView;
@property (strong, nonatomic) NSMutableArray *listArray;
@end


@implementation LockedChatsTableView

- (void)viewDidLoad {

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showPicker:)];
    self.navigationItem.rightBarButtonItem = addButton;
    self.navigationItem.title = @"Locked Chats";

    [self.listArray removeAllObjects];

    [self.view setBackgroundColor:[UIColor whiteColor]];
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    self.tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    self.tabView.alwaysBounceVertical = NO;
    self.tabView.delegate = self;
    self.tabView.dataSource = self;

    self.listArray = [[NSMutableArray alloc] init];

    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:[self path]];
    self.listArray = [data objectForKey:@"LockedChats"];
    [self.view addSubview:self.tabView];
}

// .... some tableview delegate method ....


-(void)addChatWithName:(NSString *)chatName {
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:[self path]];
    NSMutableArray *lockedChats = [data objectForKey:@"LockedChats"];

    [lockedChats addObject:chatName];
    [data setObject:lockedChats forKey:@"LockedChats"];
    [data writeToFile:[self path] atomically:YES];       
}

-(void)removeChatWithName:(NSString *)chatName {
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:[self path]];
    NSMutableArray *lockedChats = [data objectForKey:@"LockedChats"];

    [lockedChats removeObject:chatName];

    [data setObject:lockedChats forKey:@"LockedChats"];
    [data writeToFile:[self path] atomically:YES];
}


// ====================== Contact Picker ======================== //

- (void)showPicker:(UIBarButtonItem *)sender {
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;

    [self presentViewController:picker animated:YES completion:nil];
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {

    NSString* name = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString* lastname = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);

    [self addChatWithName:[NSString stringWithFormat:@"%@ %@",name,lastname]];

    [self dismissViewControllerAnimated:YES completion:nil];

   // [self.tabView performSelectorOnMainThread:@selector(reloadData) withObject:nil];
   // [self.tabView reloadData]; 
    return NO;
}

// .... some other people picker delegate method ....

@end

所以我的问题是,当我选择一个联系人并将其名称保存到数组时,UITableView没有重新加载!我已经尝试了每一个解决方案:

  • 我在很多不同的地方试过[self.tabView reloadData];而没有运气(也许是因为它在主线之外?)

  • 我试过[self.tabView performSelectorOnMainThread:@selector(reloadData) withObject:nil];也没有重新加载表格。

  • 我尝试并阅读了stackoverflow上找到的许多其他答案但没有运气

我不知道并且整天都在处理这个小问题!非常感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

要更新表格,您需要按此顺序:

  1. 使用新信息
  2. 更新数据源(self.listArray
  3. 在主线程
  4. 上的表格上调用reloadData

    您对reloadData的来电无效,因为addChatWithName:removeChatWithName:只是写入文件;他们没有更新self.listArray。因此,当您致电reloadData时,无法显示新数据。


    与您的问题无关的其他一些小问题:

    @property (strong, nonatomic, retain) UITableView *tabView;
    

    您应该删除retain(这意味着与strong相同,而且它是多余的)。此外,我不会称之为tabView,因为未来的读者可能会认为它是UITabBar

    [self.listArray removeAllObjects];
    

    您可以删除此行。 self.listArray尚未初始化,因此无效。