所以,我有一个UITableView
加载在一个UIViewController
. 该表从NSMutableArray
我在其中添加了一个UIBarButtonItem
将NavigationBar
打开一个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上找到的许多其他答案,但没有运气
我一头雾水,整天都在研究这个小问题!非常感谢您的帮助。