2014-04-24 1 views
0

나는 하나의 uitableview에서 다른 섹션의 인구 주위에 내 머리를 포장 할 수없는 것. 현재 설정으로 할 수있는 방법이 있습니까? 필요한 경우 배열을 설정하는 방식을 바꾸려고합니다. 여기에 많은 쓸모없는 코드가있어 나중에 제거 하겠지만 기본적으로 모든 코드가 작동합니다. 문제는 모든 섹션에 NSMutableDictionary에 저장된 모든 데이터가로드되고 있다는 것입니다.현재 설정으로 uitableview를 다른 섹션으로 사용자 정의하려면 어떻게합니까?

- (void)viewDidLoad 
{ 
    bannerIsVisible = NO; 
    bannerView.hidden = YES; 
    [super viewDidLoad]; 

    //NSDate Info 

    self.secondsPerDay = 86400; 
    self.todayDate = [[NSUserDefaults standardUserDefaults]objectForKey:@"todayDate"]; 
    self.dateFormat = [[NSDateFormatter alloc] init]; 
    [self.dateFormat setDateFormat:@"MMMM dd, yyyy"]; 


    self.todayString = [self.dateFormat stringFromDate:self.todayDate]; 


    //change to string and set to the string properties 
    todaysDate.text = self.todayString; 


    //initialize the arrays and the state 
    self.mainArray = [[NSMutableArray alloc] init]; 
    self.subjectArray = [[NSMutableArray alloc]init]; 
    self.mainDictionary = [[NSMutableDictionary alloc]init]; 


    /*The UITapGestureRecognizer will make it so the program can dismiss 
    the keyboard at will. */ 
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] 
              initWithTarget:self 
              action:@selector(hideKeyboard)]; 
    [self.view addGestureRecognizer:tapGesture]; 

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]) 
    { 
     // app already launched 

     [self.subjectArray addObjectsFromArray:[[NSUserDefaults standardUserDefaults]arrayForKey:[NSString stringWithFormat:@"%@sections",self.todayString]]]; 

     [[NSUserDefaults standardUserDefaults]synchronize]; 

    } 
    else 
    { 
     self.counter++; 
     // This is the first launch ever 
     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"]; 
     [[NSUserDefaults standardUserDefaults] synchronize]; 

     AG_Storage *store = [[AG_Storage alloc] init]; 
     store.itemName = @"Swipe to Delete"; 
     NSString *storeString = store.itemName; 
     self.counter++; 

     NSLog(@"counter%d",self.counter); 

     AG_Storage *store2 = [[AG_Storage alloc] init]; 
     store2.itemName = @"+ Button to Add"; 
     NSString *store2String = store2.itemName; 
     self.counter++; 


     NSString *stringStuff = @"Tap this area to add notes for the day!"; 
     [[NSUserDefaults standardUserDefaults]setObject:stringStuff forKey:[NSString stringWithFormat:@"%@textView",self.todayString]]; 
     [[NSUserDefaults standardUserDefaults]synchronize]; 



     [self.subjectArray addObject:@"Test"]; 
     [[NSUserDefaults standardUserDefaults]setObject:self.subjectArray forKey:[NSString stringWithFormat:@"%@sections",self.todayString]]; 
     [[NSUserDefaults standardUserDefaults]synchronize]; 


     //allocates tempDic and gives it tasks and keys 
     NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys: storeString, [NSString stringWithFormat:@"%d",0], store2String, [NSString stringWithFormat:@"%d",1], nil]; 



     //Sets tempDic in mainDictionary with the key Test 
     [self.mainDictionary setObject:tempDic forKey:[NSString stringWithFormat:@"%@",[self.subjectArray objectAtIndex:0]]]; 


     //Saves mainDictionary 
     [[NSUserDefaults standardUserDefaults] setObject:self.mainDictionary forKey:[NSString stringWithFormat:@"mainDictionary%@",self.todayString ]]; 
     [[NSUserDefaults standardUserDefaults]synchronize]; 


    } 

    [self loadInitialData]; 

} 



- (void)loadInitialData 
{ 
    // Do any additional setup after loading the view, typically from a nib. 


    //call my custom class and store today's date. 
    AG_Storage *theDateToday = [[AG_Storage alloc]init]; 
    theDateToday.todaysDate = self.todayDate; 

    NSLog(@"tried loadinitialdata"); 
    NSMutableDictionary *mutDic = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"mainDictionary%@",self.todayString ]]; 
    NSLog(@"%@",mutDic); 
    //Populate mainArray 
    for (int x= 0; x < self.subjectArray.count; x++) { 


     for (int y = 0; y != -99; y++) { 

      NSDictionary *tempDir = [mutDic valueForKey:[NSString stringWithFormat:@"%@",[self.subjectArray objectAtIndex:x]]]; 

      [[NSUserDefaults standardUserDefaults] synchronize]; 

      NSLog(@"tempDir %@",tempDir); 
      NSString *tempString = [tempDir valueForKey:[NSString stringWithFormat:@"%d",y]]; 
      NSLog(@"tempString %@",tempString); 
      if ([tempString length] != 0) { 
       //add the data to the mainArray and update counter 
       [self.mainArray addObject:tempString]; 
       NSLog(@"added to array%@", self.mainArray); 
      } 
      else 
       y = -100; 


     } 
     NSLog(@"SHOULD EXIT LOOP RIGHT NOW"); 


    } 

    NSLog(@"LOOP ENDED PROPERLY"); 




    /*Populate textviews which hold the user's notes. Populates 
    based on the state of the program.*/ 
    todayTextView.text = [[NSUserDefaults standardUserDefaults]stringForKey:[NSString stringWithFormat:@"%@textView",self.todayString]]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 


} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return self.subjectArray.count; 

} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return self.mainArray.count; 
} 

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

    return [self.subjectArray objectAtIndex:section]; 
} 



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


    //populates the table based on which view is selected. 

    UITableViewCell *cell = [tableViewer dequeueReusableCellWithIdentifier:@"todayCell"]; 
    NSString *toDoItem = [self.mainArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = toDoItem; 

    cell.textLabel.adjustsFontSizeToFitWidth = YES; 
    cell.textLabel.minimumScaleFactor = 0.5; 



    return cell; 



} 

답변

1

섹션을 재사용하고 코드 설정을 유지하려고하기 위해, 나는 [NSMutableArray][NSMutableArray]s의 사용을 권장합니다. 각 NSMutableArray 그런 다음 tableView:numberOfRowsInSection:에서 당신이 NSInteger PARAM를 사용하여 인덱스의 배열을 잡아 indexsection

에 해당 테이블의 섹션을 나타내는 것입니다.

인사이드 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath indexPath.section을 통해 섹션에 액세스하고 섹션의 NSMutableArray에서 데이터를 추출 할 수 있습니다. 그런 다음 해당 섹션의 데이터에만 액세스하여 행을 만들 수 있습니다.

예 :

self.sectionArrays = [NSMutableArray new]; 

.... 


(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [self.sectionArrays count]; 

} 

(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [[self.sectionArrays objectAtIndex:section] count]; 
} 

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSArray *currentSectionArray = [self.sectionArrays objectAtIndex:indexPath.section]; 
    ... 
    NSString *toDoItem = [currentSectionArray objectAtIndex:indexPath.row]; 
    ... 
    configure cell 
} 
+0

흠, 그래서 난'mainArray'와'sectionArray'에서 두 개체와'sectionArrays'을 채우는 무엇입니까? 나는 아직도 조금 잃어버린 하하 야. – user3430084

+0

당신이 당신의 물건을 너무 많이 나눠주고있는 것처럼 보입니다. 나는 그것이'mainArray'와'sectionArray' 둘 모두로부터 객체를 포함해야한다고 생각하지만, 당신의 코드는 한눈에 완전히 파악하기에 약간 조밀합니다. 당신은 이것을하고 싶습니다 :'sectionArray [0]'에 해당하는'mainArray'의 모든 객체를 가져옵니다. 그것은 배열에 넣어지고'sectionArrays [0]'으로 설정됩니다. 마찬가지로,'sectionArrays [1]'='sectionArray [1]' – LyricalPanda

+0

에 속한'mainArray'의 모든 객체와'sectionArray'에 의해'subjectArray'를 의미합니다 :). 섹션을 사용하려면 기본적으로'mainArray'를 작은 조각 (즉, 서브 어레이)으로 잘 자르면됩니다. – LyricalPanda

관련 문제