0

我正在尝试创建一个帐户页面,用户可以在其中选择他喜欢的语言和语音。对于我的应用程序的这一部分,我正在考虑创建 2 个不同的 UIButton,一旦用户按下其中一个,UITableView 将出现在适当的位置tableFrame(类似于下拉菜单)。最初,当我只有一个阵列时,一切都很好。现在,我有一个通用数组,用于从表视图委托方法加载数据和 2 个包含语言和语音的其他数组。

我的问题是,当我第一次点击任何按钮时,表格不显示任何内容但numberOfRowsInSection返回正确的值。接下来,如果再次点击相同或另一个按钮..一切都很好。

我相信问题出现在我最初的位置,初始化没有对象的通用数组viewDidLoad

我已经尝试过reloadData,现在成功了。

有任何想法吗?在我的代码下面:

帐户.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

帐户.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
4

1 回答 1

0

content”是一个空的可变数组,直到您触摸调用“ selectLanguage”和“ selectVoice”方法的任何(按钮?)操作(然后重新分配“ content”变量)。

我没有看到对 " content" 可变数组的任何其他操作,所以对我来说,在您实际触摸某些东西之前什么都不会出现也就不足为奇了。

你也没有显示你在哪里打电话“ reloadData”。" content" 需要在其中包含数组条目才能使 " reloadData" 工作。

于 2013-06-06T22:05:57.850 回答