【问题标题】:How to make multiple list in one UITableView? IOS Swift如何在一个 UITableView 中创建多个列表? IOS 斯威夫特
【发布时间】:2015-04-20 06:35:38
【问题描述】:

有人可以帮助我吗?我将 UITableView 分割成两个 registeredunregistered 这是我的视图

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

[ { "itemName": "Glass 101", "status": 1 }, { "itemName": "Glass 102", "status": 0 }, { "itemName": "Glass 103", "status": 0 }, { "itemName": "Glass 104", "status": 1 }, { "itemName": "Glass 105", "status": 0 }, { "itemName": "Glass 106", "status": 1 } ]

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

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

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

【问题讨论】:

  • 你只有两个状态权(0或1)?
  • 将您的 JSON 过滤成两个数组 - 查看 filteredArrayUsingPredicate,然后为每个表部分使用适当的数组
  • 是的,先生 0 表示未注册,1 表示已注册 @AkashShinde
  • @Paulw11 我可以对数组进行过滤,但我不知道如何在每个表格部分显示它
  • 只需在cellForRowAtIndexPathnumberOfRowsInSection中使用适当的数组

标签: ios objective-c uitableview swift


【解决方案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;
}

【讨论】:

  • 这是一个非常漂亮的代码,用于识别哪个是哪个,谢谢这位先生。但是现在缺少的是 cellForRowAtIndexPath 中的代码,我不知道如何显示名称
  • 我正在添加这个。等待
【解决方案2】:

实现此数据源方法。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     return 2;
}

使用filteredArrayUsingPredicate 获取两个不同的数组对象,一个用于status 0,一个用于status 1

然后实现这个方法。

-(NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section{
   if(section == 0){
    return [statusZero count]
   }
   else{
   return [statusOne count]
   }
}

最后用这个方法添加单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0){
   // add status 1 array cell
}
else{
  // add status 0 array cell
}
 return cell
}

【讨论】:

    【解决方案3】:

    您可以解析您的json并根据您的条件创建两个数组status = 0,将当前索引的整个对象放入not_registered_Array,如果status == 1,将当前索引的整个对象放入registered_Array

    NSMutableArray *registered_Array = [[NSMutableArray alloc] init]; //Define this, out of any method, may be above `viewDidLoad`
    NSMutableArray *not_registered_Array = [[NSMutableArray alloc] init];
    for (int i = 0 ; i < yourRespomseArray.count ; i++) {
        if ([[[yourRespomseArray objectAtIndex:i] objectForKey:@"status"] boolValue]) {
            [registered_Array addObject:[yourRespomseArray objectAtIndex:i]];
        }
        else{
            [not_registered_Array addObject:[yourRespomseArray objectAtIndex:i]];
        }
    }
    

    从这 2 个数组中,您可以按部分返回行数并从中设置单元格值。

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        println(prefs.valueForKey("CAT_ID"))
        if (section == 0 ){
            return not_registered_Array.count
        }else{
            return registered_Array.count
        }
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell : UITableViewCell =  ...
        if (section == 0 ){
            let dicObject:NSDictionary = not_registered_Array.objectAtIndex(indexPath.row) as! NSDictionary
            cell.textLabel?.text = dicObject.valueForKey("itemName")
        }else{
            let dicObject:NSDictionary = registered_Array.objectAtIndex(indexPath.row) as! NSDictionary
            cell.textLabel?.text = dicObject.valueForKey("itemName")
        }
        return cell
    }
    

    HTH,享受编码!

    【讨论】:

    • 这就是我所缺少的!感谢您的大力帮助。
    • - 用swift编写它也应该得到一个加分
    【解决方案4】:

    我不确定您是否无法将 JSON 解析为对象,或者您不知道如何将 UITableView 拆分为多个部分。

    根据对象的使用情况,您需要解析 JSON 并使用字典中的值实例化对象(数组中的每条记录都可以简单地放入 Dictionary&lt;String, AnyObject&gt;)。例如,看这个:

    http://www.raywenderlich.com/82706/working-with-json-in-swift-tutorial

    我并不是说这是将 JSON 解析为对象的最佳方式,而是一种帮助您入门的方式。

    使用filteredArrayUsingPredicate 将数组过滤为属性上的两个数组。所以是这样的:

    let myCompleteArray = {};
    var registered = myCompleteArray.filter ({ $0.status == true });
    var unregistered = myCompleteArray.filter( { $0.status == false });
    

    然后你需要实现你的UITableViewDataSource,像这样:

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2;
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        if (section == 0) {
            return registered.count;
        }
        else {
            return unregistered.count;
        }
    }
    

    当然,您需要更改 cellForRowAtIndexPath 以检查正在呈现的部分(就像上面的 numberOfRowsInSection 一样。

    【讨论】:

    • 这条线很聪明myCompleteArray.filter ({ $0.status == true })感谢这个
    猜你喜欢
    • 2017-05-06
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2017-03-24
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    相关资源
    最近更新 更多