gpt4 book ai didi

ios - 如何使用当前设置将 uitableview 自定义为不同的部分?

转载 作者:行者123 更新时间:2023-11-29 03:01:16 25 4
gpt4 key购买 nike

我似乎无法在单个 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;



}

最佳答案

为了重用部分并尝试保留代码设置,我建议使用 [NSMutableArray][NSMutableArray]。每个 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
}

关于ios - 如何使用当前设置将 uitableview 自定义为不同的部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23276768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com