gpt4 book ai didi

ios - ABPeoplePicker 选择后重新加载 UITableView

转载 作者:行者123 更新时间:2023-11-28 21:58:01 25 4
gpt4 key购买 nike

因此,我在 UIViewController 中加载了一个 UITableView。该表从 NSMutableArray

获取数据

我在 NavigationBar 上添加了一个 UIBarButtonItem,它将打开一个 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 上找到的许多其他答案,但没有成功

我一时糊涂,一整天都在处理这个小问题!非常感谢您的帮助。

最佳答案

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

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

您对 reloadData 的调用无效,因为 addChatWithName:removeChatWithName: 仅写入文件;他们没有更新 self.listArray。因此,当您调用 reloadData 时,没有新数据可显示。


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

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

你应该删除retain(它的意思和strong是一样的,而且是多余的)。另外,我不会将此称为 tabView,因为 future 的读者可能会认为它是 UITabBar

[self.listArray removeAllObjects];

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

关于ios - ABPeoplePicker 选择后重新加载 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25894576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com