0

我有一个接收通知并填充数组的视图控制器。然后将其显示在表格视图中。问题是 tableview 只显示最后添加的对象。如果我然后推送到新视图并再次返回,整个数组将显示在表中。我认为可能在 viewDidLoad 之后调用了 receiveNotification 方法,这是有道理的。我尝试使用 [myTableView reloadData] 重新加载数据;这没有用。我也试过一个选择器,还有一个有延迟的选择器。
有没有人尝试过并且成功了,如果是这样,你能告诉我怎么做吗?谢谢您阅读此篇。

编辑: - 我知道这段代码充满了垃圾,只是我最近彻底改变了我的应用程序,在整理之前我正在处理最后一点!:)

-(void)receiveNotification:(NSNotification *)notification
{


if (!path) {


    path = [[NSBundle mainBundle] pathForResource:@"OverallDictionary" ofType:@"plist"];
    theDictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
}

NSArray *temporaryArray = [theDictionary allKeys];

for (id key in temporaryArray) {
    if ([[notification name] isEqualToString:key]) {
        if (!allCompletedTopics) {
            allCompletedTopics = [[NSMutableArray alloc] init];
        }
        if (![allCompletedTopics containsObject:[[theDictionary valueForKey:key] valueForKey:@"Title"]]) {
            [allCompletedTopics addObject:[[theDictionary valueForKey:key] valueForKey:@"Title"]];
            defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:allCompletedTopics forKey:@"hi"];
            theFinalScore++;
            [[NSUserDefaults standardUserDefaults] setInteger:theFinalScore forKey:@"theFinalScore"];
            [self updateTheProgressView];

        }
    }
}
NSLog(@"received %@", allCompletedTopics);
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTheTable" object:nil];


}


- (void)viewDidLoad
{

[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTable:) name:@"reloadTheTable" object:nil];

// Do any additional setup after loading the view.
allCompletedTopics = [[[NSUserDefaults standardUserDefaults] objectForKey:@"hi"] mutableCopy];

[defaults setObject:allCompletedTopics forKey:@"theKey"];

if (!path) {
    path = [[NSBundle mainBundle] pathForResource:@"OverallDictionary" ofType:@"plist"];
    theDictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
    //  NSLog(@"Created on view did load");
}

NSString *path1 = [[NSBundle mainBundle] pathForResource:@"Keys" ofType:@"plist"];
NSArray *theKeys = [[NSArray alloc] initWithContentsOfFile:path1];

for (NSString *theString in theKeys) {
    //  NSLog(@"%@", theString);
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:theString object:nil];

}
theFinalScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"theFinalScore"];

[self updateTheProgressView];
NSLog(@"%@", allCompletedTopics);
//   [myTableView reloadData];


}



- (void)reloadTable:(NSNotification *)notification
{
[myTableView reloadData];
NSLog(@"hi");
}

-(无效)创建数据{

if (!levelThree) {
    levelThree = [[NSMutableArray alloc] init];
}

levelThreeFinal = [[NSMutableArray alloc] initWithCapacity:48];


for (NSString *object in allCompletedTopics) {
    if (([object rangeOfString:@"level 3"].location != NSNotFound) && (![levelThree containsObject:object]))
    {
      //  NSLog(@"%@", object);

        [levelThree addObject:object];
        NSString *theTemp = [object substringToIndex:object.length - 8];
        [levelThreeFinal addObject:theTemp];


    }
    levelThreeDict = [NSDictionary dictionaryWithObject:levelThreeFinal forKey:@"Levels"];
        }


allLevels = [[NSMutableArray alloc] initWithObjects:levelThreeDict,
             //levelFourDict,
             //levelFiveDict,
             //levelSixDict,
             nil];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[[allLevels objectAtIndex:section] objectForKey:@"Levels"] count]; //返回2;}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return allLevels.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...


    //  cell.textLabel.text = [allCompletedTopics objectAtIndex:indexPath.row];

    NSDictionary *dictionary = [allLevels objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"Levels"];
    NSString *cellValue = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;

    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    NSString *theTitle = [[NSString alloc] init];

    switch (section) {
        case 0:
            theTitle = @"Level 3";
            break;
        case 1:
            theTitle = @"Level 4";
            break;
        case 2:
            theTitle = @"Level 5";
            break;
        case 3:
            theTitle = @"Level 6";
            break;
        default:
            break;
    }
    //return theTitle;

    return @"Your progress so far!";
}
4

0 回答 0