gpt4 book ai didi

ios - 如何手动设置 titleForHeaderInSection

转载 作者:行者123 更新时间:2023-11-29 13:34:15 25 4
gpt4 key购买 nike

我像这样从 plist 加载数据到 uitableview:

- (void)viewDidLoad {
[super viewDidLoad];


NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:@"data.plist"];

NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init];
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:path];


if ([[NSUserDefaults standardUserDefaults] boolForKey:@"purpleKey"])
{
NSArray *purple = [myDict objectForKey:@"Purple"];
[resultArray addObject:@"Purple"];
[resultDic setValue:purple forKey:@"Purple"];
}
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"orangeKey"])
{
NSArray *orange = [myDict objectForKey:@"Orange"];
[resultArray addObject:@"Orange"];
[resultDic setValue:orange forKey:@"Orange"];
}


self.tableData = resultDic;
self.sectionsTitle = resultArray;

}

titleForHeaderInSection

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [sectionsTitle objectAtIndex:section];
}

我的plist结构

enter image description here

我的问题是:如何在不更改 plist 文件中的名称的情况下手动设置紫色标题和橙色标题的标题?像这样:紫色 = 类别 1,橙色 = 类别 2

编辑

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return sectionsTitle.count;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [sectionsTitle objectAtIndex:section];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int num = [[tableData objectForKey:[sectionsTitle objectAtIndex:section]] count];

if (num > 3) {
num = 3;
}

return num;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

NSDictionary *dict = [[tableData objectForKey:[sectionsTitle objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

cell.textLabel.numberOfLines = 1;
cell.textLabel.font = [UIFont systemFontOfSize:11];
cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", [dict objectForKey:@"Name"], [dict objectForKey:@"Address"]];

if ([dict objectForKey:@"Address"] == (NULL)) {
//do nothing
}

return cell;
}

最佳答案

从您的代码来看,节标题来自 resultsArray,您用常量字符串 Orange 和 Purple 填充它。

您可以将不同的常量字符串放入该数组,除非我遗漏了什么。

所以,而不是

[resultArray addObject:@"Orange"];

使用

[resultArray addObject:@"Category 1"];

关于ios - 如何手动设置 titleForHeaderInSection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11053489/

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