繁体   English   中英

如何使用当前设置将uitableview定制为不同的部分?

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

我似乎无法在一个uitable视图中把头围在不同部分的周围。 有什么方法可以用当前设置完成? 如果愿意,我愿意更改阵列的设置方式。 我在这里有很多无用的代码,稍后我将删除它们,但是基本上所有的代码都可以工作。 问题是所有部分都将使用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;



}

为了重新切片,并尝试保持你的代码的设置,我会建议使用[NSMutableArray][NSMutableArray]s 每个NSMutableArray代表表的一部分,其中index对应于该section

然后在tableView:numberOfRowsInSection:使用NSInteger参数从索引中获取数组。

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath您可以通过indexPath.section访问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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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