【问题标题】:iOS: uitableview with multiple dynamic sections objective ciOS:具有多个动态部分的uitableview目标c
【发布时间】:2019-01-04 10:02:44
【问题描述】:

我收到此 API 响应我想将所有这些数据放入 UITableView 部分我为标题的标题创建一个数组但现在我正在尝试但无法为我必须显示到单元格中的内部数据创建数组. 请帮助我将这些数据填充到带有部分的 tableview 中。

   {
        "sponsers": {
            "ORGANISED BY": [
                {
                    "id": "27",
                    "sp_name": "www.****.my",
                    "sp_logo": "499270053_logo2.png",
                    "events": "24",
                    "priority": "0",
                    "status": "1",
                    "s_type": "1"
                },
                {
                    "id": "29",
                    "sp_name": "www.anderesfourdy.com",
                    "sp_logo": "157207241_my-partner.png",
                    "events": "24",
                    "priority": "0",
                    "status": "1",
                    "s_type": "1"
                },
                {
                    "id": "30",
                    "sp_name": "visitpenang.com.my",
                    "sp_logo": "730893130_logo1.png",
                    "events": "24",
                    "priority": "0",
                    "status": "1",
                    "s_type": "1"
                }
            ],
            "OFFICIAL VENUE PARTNER": [
                {
                    "id": "32",
                    "sp_name": "www.setiaspice.com",
                    "sp_logo": "2116042847_eda576900989f3222d77c7d511c77dcc.jpg",
                    "events": "24",
                    "priority": "0",
                    "status": "1",
                    "s_type": "2"
                }
            ],
            "OFFICIAL AIRLINE PARTNER": [
                {
                    "id": "33",
                    "sp_name": "www.malaysiaairlines.com/in/en.html",
                    "sp_logo": "475052144_276e3235a63924f1922e703d4a8b31a6.png",
                    "events": "24",
                    "priority": "0",
                    "status": "1",
                    "s_type": "3"
                }
            ],
            "HOTEL PARTNERS": [
                {
                    "id": "34",
                    "sp_name": "olivetreehotel.com.my",
                    "sp_logo": "97088892_3cdc275c1942eeb186d6920abd300611.jpg",
                    "events": "24",
                    "priority": "0",
                    "status": "1",
                    "s_type": "4"
                },
                {
                    "id": "35",
                    "sp_name": "penang.equatorial.com",
                    "sp_logo": "622248930_5deb8b605822d4b5cea4e122b55dcbdd.jpg",
                    "events": "24",
                    "priority": "0",
                    "status": "1",
                    "s_type": "4"
                },
                {
                    "id": "36",
                    "sp_name": "www.beatpenang.com/www.shangri-la.com/penang/rasasayangresort",
                    "sp_logo": "918729310_38d4aca7a13e8e65885aa690835219f5.png",
                    "events": "24",
                    "priority": "0",
                    "status": "1",
                    "s_type": "4"
                },
                {
                    "id": "37",
                    "sp_name": "www.beatpenang.com/www.vistanahotels.com/penang",
                    "sp_logo": "1497393994_66eb81f97563446cb2b1c743e5d69b99.jpg",
                    "events": "24",
                    "priority": "0",
                    "status": "1",
                    "s_type": "4"
                }
            ],..........

我正在使用以下代码将数据显示到 tableview 中: DataArray 包含“赞助商”键内的值

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [DataArray count];
}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [DataArray objectAtIndex:section];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[DataArray objectAtIndex:section] count];
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    cell.textLabel.font=[UIFont fontWithName: @"Arial" size: 14.0 ];

    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;

    //to hide extra cell from table
    sponsorTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    /*UIView * bkView = (UIView*)[cell viewWithTag:150];
    if(indexPath.row % 2 == 0)
    {
        bkView.backgroundColor = [UIColor whiteColor];
        //cell.backgroundColor = [UIColor whiteColor];
    }
    else
    {
        bkView.backgroundColor = [UIColor colorWithRed:234/255.0 green:236/255.0 blue:235/255.0 alpha:1];
    }*/



    NSString* name = [[DataArray objectAtIndex:indexPath.row]valueForKey:@"sp_name"];
    NSLog(@"-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=%@",name);
    UILabel * nameLbl = (UILabel *)[cell viewWithTag:102];
    nameLbl.text = name;



    NSString * urlstring = [[DataArray objectAtIndex:indexPath.row]valueForKey:@"sp_logo"];
    NSLog(@"urlStr is %@",urlstring);
    NSString * completeString = [NSString stringWithFormat:@"http:****************",urlstring];
    NSLog(@"Complete Url string is %@",completeString);
    UIImageView * imgVw = (UIImageView *)[cell viewWithTag:101];

    cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width / 2;
    cell.imageView.clipsToBounds = YES;

    NSString *imageUrl = [NSString stringWithFormat:@"%@",completeString];
    dispatch_queue_t imageDownloadQueue = dispatch_queue_create(DISPATCH_QUEUE_PRIORITY_DEFAULT, nil);

    dispatch_async(imageDownloadQueue, ^{
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];

        dispatch_async(dispatch_get_main_queue(), ^{
            UIImage * image1 = [UIImage imageWithData:imageData];
            imgVw.image = image1;
        });
    });


    return cell;
}

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    您在这里拥有的是(用NSJSONSerialization 转换)是sponsors 是一个字典,其中每个字典都包含一个数组。

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        NSDictionary *sponsers = parsedResponse[@"sponsers"];
        return sponsers.allKeys.count; // For every key we need a section
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        NSDictionary *sponsers = parsedResponse[@"sponsers"];
        NSString *key = sponsers.allKeys[section];
        return key; // The key is the section name
    
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        NSDictionary *sponsers = parsedResponse[@"sponsers"];
        NSString *key = sponsers.allKeys[section];
        NSArray *content = sponsers[key]; // Each item should be an array
        return content.count; // Return number of items in embedded array
    }
    
    - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSDictionary *sponsers = parsedResponse[@"sponsers"];
        NSString *key = sponsers.allKeys[indexPath.section];
        NSArray *content = sponsers[key];
        NSDictionary *item = content[indexPath.row];
        // We have the item. Populate the cell
        ... generate cell ...
        cell.textLabel.text = item[@"sp_name"];
        ...
    }
    

    这应该是构建表格视图所需的所有信息。如果您在填充单元格数据时遇到问题,我建议您再问一个具体的问题。

    【讨论】:

    【解决方案2】:

    您必须设置特定的键,并且该键必须存储“部分值”。因此您可以轻松获取部分的值。您的字典设置如下所示。

    "data" : [
        {
          “Section Title“ : “Section1”,
          “InnerCellData” : [
            {
            //record 1
            }
           {
            //record 2
            }
          ]
        }
        {
          “Section Title“ : “Section2”,
          “InnerCellData” : [
            {
            //record 1
            }
           {
            //record 2
            }
          ]
        }
    
      ]
    

    它适合你。

    【讨论】:

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