0

所以,我有一个UITableView加载在一个UIViewController. 该表从NSMutableArray

我在其中添加了一个UIBarButtonItemNavigationBar打开一个ABPeoplePickerNavigationController以选择一个联系人并将其名称添加NSMutableArray到要显示在UITableView

这是我的代码的摘要

@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上找到的许多其他答案,但没有运气

我一头雾水,整天都在研究这个小问题!非常感谢您的帮助。

4

1 回答 1

2

要更新表,您需要按以下顺序:

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

您的调用reloadData不起作用,因为addChatWithName:并且removeChatWithName:只是写入文件;他们没有更新self.listArray。因此,当您调用 时reloadData,没有要显示的新数据。


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

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

您应该删除retain(它与 的含义相同strong,并且是多余的)。另外,我不会这样称呼它tabView,因为未来的读者可能会认为它是UITabBar.

[self.listArray removeAllObjects];

您可以删除此行。self.listArray甚至还没有初始化,所以这什么都不做。

于 2014-09-17T15:33:41.040 回答