我似乎无法在单个uitableview中围绕不同部分的人群。我目前的设置有什么方法可以做到吗?如果需要,我愿意改变我的阵列设置方式。我在这里有很多无用的代码,我将在稍后删除,但基本上它都可以工作。问题是所有部分都加载了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;
}
答案 0 :(得分:1)
为了重复使用部分并尝试保留代码设置,我建议您使用[NSMutableArray]
[NSMutableArray]s
。每个NSMutableArray
代表您表格的一部分,其中index
对应于section
然后在tableView:numberOfRowsInSection:
中使用NSInteger
参数从索引中获取数组。
在(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
}