ios - 如何在一个 UITableView 中制作多个列表? iOS swift

标签 ios objective-c uitableview swift

谁能帮帮我。我将 UITableView 分为两个 registeredunregistered 这是我的 View

enter image description here

但我不知道如何将我的 json 数据绘制到其中。这是我的数据

[ { "itemName": "玻璃 101", “状态”:1 }, { "itemName": "玻璃 102", “状态”:0 }, { "itemName": "玻璃 103", “状态”:0 }, { "itemName": "玻璃 104", “状态”:1 }, { "itemName": "玻璃 105", “状态”:0 }, { "itemName": "玻璃 106", “状态”:1 } ]

如果状态等于 0 我需要做什么 我把它放在 not registered 然后如果 1 我把它放在 已注册

我现在拥有的是简单列表,一个包含已注册和未注册的列表。我想知道我怎样才能做到这一点。

任何评论或建议或链接都​​会有很大帮助,因为我不知道如何开始。提前致谢

最佳答案

假设您的数据存储在名为 tempArray 的数组中。然后这样做:

NSMutableArray *registered = [[NSMutableArray alloc] init];
NSMutableArray *notRegistered = [[NSMutableArray alloc] init];
for (int i = 0 ; i < tempArray.count ; i++) {
    if ([[[tempArray objectAtIndex:i] objectForKey:@"status"] boolValue]) {
        [registered addObject:[tempArray objectAtIndex:i]];
    }
    else{
        [notRegistered addObject:[tempArray objectAtIndex:i]];
    }
}

然后在 UITableView 的 numberOfRowsInSection 中执行此操作:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 1){
        return registered.count;
    }
    return notRegistered.count;
}

然后在 cellForRowAtIndexPath 中执行此操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
    }
    if (indexpath.section == 0) {
        cell.textLabel.text = [[notRegistered objectAtIndex:indexpath.row] objectForKey:@"itemName"];
    }
    else {
        cell.textLabel.text = [[registered objectAtIndex:indexpath.row] objectForKey:@"itemName"];
    }
    return cell;
}

关于ios - 如何在一个 UITableView 中制作多个列表? iOS swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29740757/

相关文章:

ios - 当用户在文本字段中键入时更新输出

iphone - 大于特定月份的 SQLite 日期时间

通过 Objective-C 消息传递访问 C 风格对象数组的正确语法?

ios - 通过 Picker View 选择切换到哪个 ViewController

ios - UITableView 行在重新排序时快速重复

ios - 是否可以在 iOS 设备上获取设备 iTunes 商店

jquery - 在 iPad 上模仿基于 Flash 的应用程序的最佳方法是什么?

ios - 在 objective-c 中使用异步代码避免嵌套 block

ios - 加载图像后调整 UITableViewCell 的大小

ios - 将 tableview 添加到 scrollview 并让 scrollview 在单元格添加到 tableview 时保持高度增长