Я пытаюсь создать страницу учетной записи, где пользователь сможет выбрать предпочтительный язык и голос. Для этой части моего приложения я думал создать 2 разных UIButtons, и как только пользователь нажал один из них, UITableView появится в соответствующем месте tableFrame
(что-то вроде выпадающего меню). Сначала все было отлично, когда у меня был только один массив. Теперь у меня есть общий массив, который используется в методах делегата Table View для загрузки данных и двух других массивов, содержащих языки и голоса.Содержимое UITableView не отображается в первый раз даже после reloadData
Моя проблема в том, что в первый раз, когда я нажимаю на любую из кнопок, таблица не отображает содержимое, но numberOfRowsInSection
возвращает правильное значение. Затем, если снова нажмите на ту же или другую кнопку. Все в порядке.
Я считаю, что проблема возникает, когда я есть, изначально инициализировать общий массив без объекта в viewDidLoad
.
Я пробовал reloadData
с успехом.
Любые идеи? Ниже мой код:
Account.h
#import <Foundation/Foundation.h>
@interface AccountViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
NSMutableArray *content;
NSMutableArray *languages;
NSMutableArray *voices;
CGRect tableFrame;
}
@property (strong, nonatomic) IBOutlet UIButton *languageBtn;
@property (strong, nonatomic) IBOutlet UIButton *narratorBtn;
@property (strong, nonatomic) IBOutlet UITableView *contentTbl;
- (IBAction)selectLanguage:(id)sender;
- (IBAction)selectVoice:(id)sender;
@end
Account.m
- (void) viewDidLoad{
[super viewDidLoad];
content = [[NSMutableArray alloc] init];
languages = [[NSMutableArray alloc] initWithObjects:@"English", @"Ελληνικά", @"French", @"Spanish", @"Deutch", nil];
voices = [[NSMutableArray alloc] initWithObjects:@"English", @"US English", @"Ελληνικά", @"Spanish", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [content count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [content objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)selectLanguage:(id)sender{
if ([(UIButton *)sender tag] == 0) {
content = [languages copy];
tableFrame = CGRectMake(50, 173, 220, 203);
[self startAnimationAndDisplayTable:YES];
[(UIButton *)sender setTag:1];
} else{
[self startAnimationAndDisplayTable:NO];
[(UIButton *)sender setTag:0];
}
}
- (IBAction)selectVoice:(id)sender{
if ([(UIButton *)sender tag] == 0) {
content = [voices copy];
tableFrame = CGRectMake(50, 243, 220, 203);
[self startAnimationAndDisplayTable:YES];
[(UIButton *)sender setTag:1];
} else{
[self startAnimationAndDisplayTable:NO];
[(UIButton *)sender setTag:0];
}
}
- (void)startAnimationAndDisplayTable:(BOOL)show{
if (show) {
[self.contentTbl setHidden:NO];
[self.contentTbl setFrame:tableFrame];
[UIView beginAnimations:@"Fade In" context:nil];
[UIView setAnimationDuration:0.7];
[self.contentTbl setAlpha:1.0];
[UIView commitAnimations];
[self.contentTbl reloadData];
} else{
[UIView beginAnimations:@"Fade Out" context:nil];
[UIView setAnimationDuration:0.5];
[self.contentTbl setAlpha:0.0];
[UIView commitAnimations];
}
}
@end
Благодарим за быстрый ответ. Я пропустил последний метод, где появляется 'reloadData'. Я обновлю свой вопрос ... Я меняю значения 'content', как только пользователь нажимает кнопку, а затем вызывает' reloadData' внутри 'startAnimationAndDisplayTable'. Используя этот метод, потому что я не знаю, как передать другой массив в методе делегата 'cellForRowAtIndexPath' во время выполнения (я тоже не знаю, возможно ли это). – OutOfBoundsException