【问题标题】:How to show the json sub values in to separate section in uitableview?如何在uitableview的单独部分中显示json子值?
【发布时间】:2015-08-04 06:16:50
【问题描述】:

我尝试了多种方法,我不知道。谁能告诉我实现这一点的方法。

json 值:

    dict=@{
           @"car":@[@"hatchbag",@"sedan",@"suv"],
           @"waterpurifier":@[@"one litre",@"two litre",@"three litre"]

          };

我的代码:

   subArray=[dict objectforkey:@"car"];

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section
    NSLog(@"expand section %d",expandSection);
    if (expandSection == section) {
        return [subArray count]+1;

    }
    else
        return 1;        
    }
 }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    // Configure the cell...
    if (indexPath.row == 0) {

        cell.textLabel.text = [arrPlayList objectAtIndex:indexPath.section];
        cell.imageView.image = [UIImage imageNamed:@"twitter_icon_29x29.png"];            
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }

说明: 我已将 json 值存储到单独的数组中,但我不清楚如何分隔单元格,如何将这些数据显示到单独的单元格朋友中。

Header:
car
->hatchbag
->sedan
->suv
waterpurifier
->0ne litre
->two litre
->three litre 

我怎样才能像这样在表格视图中显示数据。

【问题讨论】:

    标签: ios objective-c uitableview nsdictionary nsindexpath


    【解决方案1】:

    您应该使用表格视图中的部分将相关数据组合在一起。

    试试这个代码:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
        // Return the number of rows in the section
        if (section==0) {
            return [[dict objectForKey:@"car"] count];
        }
        else
        {
            return [[dict objectForKey:@"waterpurifier"] count];
        }
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    
        // Configure the cell...
        if (indexPath.section == 0) {
    
            cell.textLabel.text = [[dict objectForKey:@"car"] objectAtIndex:indexPath.row];
            cell.imageView.image = [UIImage imageNamed:@"twitter_icon_29x29.png"];
        }
        else if (indexPath.section == 1)
        {
            cell.textLabel.text = [[dict objectForKey:@"waterpurifier"] objectAtIndex:indexPath.row];
        }
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 2;// otherwise use dynamic condition like this way [[dict allKey] count]; -> return number of section 
    }
    
    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        if (section==0) {
            return [dict objectForKey:@"car"];
        }
        else
        {
            return [dict objectForKey:@"waterpurifier"];
        }
    }
    

    【讨论】:

    • 啊!错过了一分钟的回答:) 干得好。
    • 在 [[dict objectforkey:@"car"] objectAtIndex:indexPath.row] 这个对象和 indexpath 没有来找我之后
    • 在编码 objectAtIndex:indexPath.row 时没有出现它显示一些错误
    • 的兄弟@DharmeshDhorajiya
    【解决方案2】:

    * 斯威夫特 3.1 此代码适用于 swift 3.1 尝试一下,也许会有帮助。

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    // Return the number of rows in the section
    if section == 0 {
    
        return dict["car"].count()
    }
    else {
    
        return dict["waterpurifier"].count()
    }
    

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "Cell")
    
    // Configure the cell...
    if indexPath.section == 0 {
    
        cell?.textLabel?.text = dict["car"][indexPath.row]
        cell?.imageView?.image = UIImage(named: "twitter_icon_29x29.png")
    }
    else if indexPath.section == 1 {
    
        cell?.textLabel?.text = dict["waterpurifier"][indexPath.row]
    }
    
    cell?.accessoryType = .disclosureIndicator
    

    }

    func numberOfSections(in tableView: UITableView) -> Int {

    return 2
    
    // otherwise use dynamic condition like this way [[dict allKey] count]; -> return number of section 
    

    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    if section == 0 {
    
        return dict["car"]
    }
    else {
    
        return dict["waterpurifier"]
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多