0

だから、私は にUITableViewロードされていUIViewControllerます。テーブルはデータを取得しますNSMutableArray

に を追加しUIBarButtonItemました。NavigationBarこれABPeoplePickerNavigationControllerにより、連絡先を選択し、その名前を に追加してNSMutableArrayUITableView

これは私のコードの要約です

@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 に答える