如何在uitableview中显示和隐藏数组

Posted

技术标签:

【中文标题】如何在uitableview中显示和隐藏数组【英文标题】:How to show and hide array in uitableview 【发布时间】:2012-06-14 18:25:12 【问题描述】:

我试图像这样加载我的Arrays,但它只加载了第一个Array,而其他的没有显示。

    - (void)viewDidLoad 
    [super viewDidLoad];


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

    NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:path];



    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blackKey"])
    
        NSArray *black = [myDict objectForKey:@"Black"];
        self.tableData = [black copy];
    
    else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"greenKey"])
    
        NSArray *green = [myDict objectForKey:@"Green"];
        self.tableData = [green copy];
    
    else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"purpleKey"])
    
        NSArray *purple = [myDict objectForKey:@"Purple"];
        self.tableData = [purple copy];
    
    else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"orangeKey"])
    
    NSArray *orange = [myDict objectForKey:@"Orange"];
        self.tableData = [orange copy];
    
    else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blueKey"])
    
        NSArray *blue = [myDict objectForKey:@"Blue"];
        self.tableData = [blue copy];
    
    else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"redKey"])
    
        NSArray *red = [myDict objectForKey:@"Red"];
        self.tableData = [red copy];
    
    else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"darkblueKey"])
    
        NSArray *darkblue = [myDict objectForKey:@"Darkblue"];
        self.tableData = [darkblue copy];
    
    else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"yellowKey"])
    
        NSArray *yellow = [myDict objectForKey:@"Yellow"];
        self.tableData = [yellow copy];
    


    

    我做错了什么?

    另外,我想将数组标题显示为节标题,它是如何做到的 完成了吗?

    有没有办法限制每个部分的行数(我的数组)?

编辑

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    return [tableData count];




// 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 objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", [dict objectForKey:@"Name"], [dict objectForKey:@"Address"]];

    return cell;

EDIT2

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
    [sectionsTitle objectForKey:[sectionTitle objectAtIndex:indexPath.section]];

使用未声明的标识符“indexPath”;你的意思是“NSIndexPath”吗?

【问题讨论】:

【参考方案1】:

由于您要将它们划分为多个部分,因此您的 tableData 应该是 NSMutableDictionary 您还需要保存字典的键数组 像这样

NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init];
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:path];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"blackKey"])

    NSArray *black = [myDict objectForKey:@"Black"];

    //Add the key here
    [resultArray addObject:@"Black"];
    //Add the key and value
    [resultDic setValue:black forKey:@"Black"];

else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"greenKey"])

    NSArray *green = [myDict objectForKey:@"Green"];

    //Add the key here
    [resultArray addObject:@"Green"];
    //Add the key and value
    [resultDic setValue:black forKey:@"Green"];

//Repeat for all the elements
....
//Then after you finished all the tests and additions

//Assign the result array to your tableData
self.tableData = resultDic;
self.sectionsTitle = resultArray;

现在使用sectionsTitletableData 来填满您的表格 对于表格中的任何部分,您都可以从[sectionsTitle objectAtIndex:sectionIndex]; 获取其标题 要获取部分中的所有元素,请使用 [sectionsTitle objectForKey:[sectionTitle objectAtIndex:sectionIndex]];

【讨论】:

谢谢,看来可以了!你能帮我解决其他问题吗?如何将我的数组(黑色、绿色)设置为部分标题? 当我将 tableData 设置为 NSMutableDictionary 时,我遇到了 ARC 问题“'NSMutableDictionary' 没有可见的@interface 声明选择器 'objectAtIndex:'” 在您的 cellForRow 中,您需要更改以下 NSDictionary *dict = [tableData objectAtIndex:indexPath.row]; cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", [dict objectForKey:@"Name"], [dict objectForKey:@"Address"]];到 NSDictionary *dict = [tableData objectForKey:[sectionTitle objectAtIndex:indexPath.row]]; cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", [dict objectForKey:@"Name"], [dict objectForKey:@"Address"]]; 好的..我得到了最后一个问题..使用未声明的标识符“sectionIndex”和“sectionTitle”。非常感谢您的帮助! 很高兴:),如果有用请不要忘记接受答案:)

以上是关于如何在uitableview中显示和隐藏数组的主要内容,如果未能解决你的问题,请参考以下文章

如何隐藏 UITableView Header 然后让它重新出现?

iOS:重新加载 UITableView 后滑动显示数据

如何在 UITableView 中显示带有视频图像的视频文件?

如何在 UITableView 中显示/隐藏 UISearchController searchBar?

UISearchDisplayController 不再隐藏其下方的完整 UITableView

如何在静态 UITableView 中插入/隐藏/显示行?