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

标签 ios xcode arrays uitableview hide

我尝试像这样加载我的Array,但它只加载第一个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];
    }


    }
  1. 我做错了什么?

  2. 另外,我想将数组标题显示为节标题,如何实现 完成了吗?

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

编辑

// 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;
}

编辑2

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

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

最佳答案

由于您要将它们分为几个部分,因此您的 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]];

关于ios - 如何在uitableview中显示和隐藏数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11039115/

相关文章:

ios - 高斯模糊滤镜反转 : iOS

javascript - 将 json 文件从 GO 服务器提供给 javascript 客户端

ios - 使用类型方法时出错

ios - imageview 未相应更新

ios - 带有自定义字体的 UITextField secureTextEntry 项目符号?

xcode - 在 Playground 中导入框架时出错

ios - NavigationController 中的 TabBarController 不会在 viewControllers 中显示 TabBar

php - CAKEPHP:使用两个模型使用一个 View 和一个 Controller 在一张表中显示数据错误: undefined variable :total_hours

C++ 如何获取存储在字符串数组中的子文件夹名称?

ios - UIImagePickerControllerDelegate didFinishPickingMediaWithInfo 给我一个错误