如何使numberOfRowsInSection成为数组中数组的长度

时间:2014-09-18 21:34:19

标签: ios objective-c json

我目前设置UITableView的方式_matchCenterArray定义如下:

- (void)viewDidAppear:(BOOL)animated
{
    self.matchCenterArray = [[NSArray alloc] init];

    // Delay to allow MatchCenter item enough time to be added before pinging ebay
    [NSThread sleepForTimeInterval:2];

    [PFCloud callFunctionInBackground:@"MatchCenter"
                       withParameters:@{}
                                block:^(NSArray *result, NSError *error) {

                                    if (!error) {
                                        _matchCenterArray = result;
                                        [_matchCenter reloadData];

                                        NSLog(@"Result: '%@'", result);
                                    }
                                }];
}

result是一个数组数组,通常如下所示:

Result: '(
        {
        "Top 3" =         (
                        {
                "Image URL" = "http://thumbs4.ebaystatic.com/m/mu05CM1bactFTAWZjesohNg/140.jpg";
                "Item URL" = "http://www.ebay.com/itm/Nexus-7-Latest-Model-16GB-Wi-Fi-7in-Black-2nd-Generation-2013-/281443895415?pt=US_Tablets";
                Price = "170.0";
                Title = "Nexus 7 (Latest Model) 16GB, Wi-Fi, 7in - Black (2nd) Generation 2013";
            },
                        {
                "Image URL" = "http://thumbs4.ebaystatic.com/m/mjkaCfDBdTQ6S-VTD0kFimA/140.jpg";
                "Item URL" = "http://www.ebay.com/itm/Asus-Google-Nexus-7-2nd-Generation-Tablet-7-16GB-Android-4-3-/351173222631?pt=US_Tablets";
                Price = "165.59";
                Title = "Asus Google Nexus 7 2nd Generation Tablet 7\" 16GB Android 4.3 ";
            },
                        {
                "Image URL" = "http://thumbs4.ebaystatic.com/m/mjIENDWxrHTfcrO_Tmu4-zw/140.jpg";
                "Item URL" = "http://www.ebay.com/itm/Google-Nexus-7-16GB-HD-K008-NEXUS7-ASUS-2B16-2nd-Gen-Tablet-Priority-Ship-/301315740555?pt=US_Tablets";
                Price = "164.99";
                Title = "Google Nexus 7 16GB HD K008 NEXUS7 ASUS-2B16 2nd Gen Tablet Priority Ship";
            },
                        {
                "Search Term" = "nexus 7 16gb";
            }
        );
    },
        {
        "Top 3" =         (
                        {
                "Image URL" = "http://thumbs3.ebaystatic.com/m/mbSjXw608gtlLhYC5GbrbOg/140.jpg";
                "Item URL" = "http://www.ebay.com/itm/rolex-datejust-/301318985642?pt=Wristwatches";
                Price = "400.0";
                Title = "rolex datejust";
            },
                        {
                "Image URL" = "http://thumbs3.ebaystatic.com/m/mx2eBCLXBEY30DXIIMrm_MQ/140.jpg";
                "Item URL" = "http://www.ebay.com/itm/VINTAGE-LADIES-ROLEX-STANDARD-17J-GOLD-FILLED-WATCH-WORKING-NICE-/111466728842?pt=Wristwatches";
                Price = "349.99";
                Title = "VINTAGE LADIES ROLEX STANDARD 17J GOLD FILLED WATCH WORKING NICE  ";
            },
                        {
                "Image URL" = "http://thumbs2.ebaystatic.com/m/mj7-GblU-4Al2X5q0sRvkfw/140.jpg";
                "Item URL" = "http://www.ebay.com/itm/Rolex-17000-Quartz-Datejust-BLACK-DIAL-5035-/271610085029?pt=Wristwatches";
                Price = "400.0";
                Title = "Rolex 17000 Quartz Datejust  BLACK DIAL 5035";
            },
                        {
                "Search Term" = rolex;
            }
        );
    }
)'

对于我表中的部分数量,我将其设置为采用“前3个”数组的数量,如下所示:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _matchCenterArray.count;
}

我想对每个部分中的行数做同样的事情,但我不确定如何为此设置语法。我希望它是每个“前3”阵列的长度。我目前将其设置为return 3作为临时搭建,但由于eBay的结果,“前三名”并不能保证实际拥有3,所以我不想让它变成这样的硬数字。

2 个答案:

答案 0 :(得分:1)

我们试试吧。 假设:你的matchCenterArray将具有以下结构:

_matchCenterArray = @[ @[contentOfFirstTop3], @[contentOfSecondTop3] ] 

可以检索部分的数量:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _matchCenterArray.count;
}

行数:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *currentSectionArray = _matchCenterArray[section];
    return currentSectionArray.count;
}

答案 1 :(得分:0)

要获取每个部分中的行,请返回self.matchCenterArray[indexPath.section].count。这将返回该部分索引处子数组的长度。