简体   繁体   中英

How do I customize a uitableview into different sections with my current setup?

I cannot seem to wrap my head around population of different sections in a single uitableview. Is there any way I can do it with my current setup? I am willing to change the way my arrays are set up if I need to. I have a lot of useless code in here that I am going to remove later, but basically all of it works. The issue is that all sections are being loaded with all of the data stored in the NSMutableDictionary .

- (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;



}

In order to reuse sections and try to preserve your code setup, I would recommend using a [NSMutableArray] of [NSMutableArray]s . Each NSMutableArray would represent a section of your table, where the index corresponds to the section

Then in tableView:numberOfRowsInSection: you grab the array from the index using the NSInteger param.

Inside (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath you access section via indexPath.section and use that on your NSMutableArray of sections to pull out your data. From there, you then have access to data only for that section to create your rows.

Example:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM