所以,我在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上找到的许多其他答案但没有运气
我不知道并且整天都在处理这个小问题!非常感谢您的帮助。
答案 0 :(得分:2)
要更新表格,您需要按此顺序:
self.listArray
)
reloadData
醇>
您对reloadData
的来电无效,因为addChatWithName:
和removeChatWithName:
只是写入文件;他们没有更新self.listArray
。因此,当您致电reloadData
时,无法显示新数据。
与您的问题无关的其他一些小问题:
@property (strong, nonatomic, retain) UITableView *tabView;
您应该删除retain
(这意味着与strong
相同,而且它是多余的)。此外,我不会称之为tabView
,因为未来的读者可能会认为它是UITabBar
。
[self.listArray removeAllObjects];
您可以删除此行。 self.listArray
尚未初始化,因此无效。