【问题标题】:How to set titleForHeaderInSection manually如何手动设置titleForHeaderInSection
【发布时间】:2012-06-15 15:14:51
【问题描述】:

我像这样将数据从 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 结构

我的问题是: 如何在不更改 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;
}

【问题讨论】:

  • 您的意思是在应用运行时可以更改标题,还是只是想在开发过程中将其更改为不同的值?
  • 在开发过程中到不同的值

标签: ios xcode uitableview plist title


【解决方案1】:

从您的代码的外观来看,部分标题来自resultsArray,您使用常量字符串Orange 和Purple 对其进行填充。

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

所以,而不是

[resultArray addObject:@"Orange"];

使用

[resultArray addObject:@"Category 1"];

【讨论】:

  • 是的,它改变了标题,但是在那个类别之后是空的
  • 嗯,你必须在其他地方使用 resultArray 或 self.sectionTitles,作为字典或其他东西的键。添加一个仅用于标题的新数组,并保留原始数组作为键。
  • 我添加了更多代码来提问,你能帮我找到我需要更改的地方吗?
  • 就像我猜的一样——sectionTitles 有双重职责,持有键和章节标题。您应该创建一个新属性,为了便于阅读,我想它应该被称为 sectionKeys,这将包含您的橙色、紫色等,并且将在您当前使用 sectionTitles 的任何地方使用,除了您实际需要部分标题的位置。
  • 另外,重组您的数据是否有一个数组来存储所有数据。数组中的每个条目都是一个字典,其中包含一个部分名称和该部分的数据。看起来更干净。
猜你喜欢
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
  • 2021-08-21
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多