Ios UITableView未使用NSArray内容正确填充节

Ios UITableView未使用NSArray内容正确填充节,ios,objective-c,arrays,json,uitableview,Ios,Objective C,Arrays,Json,Uitableview,我的解析云代码使用以下结构将JSON发送回我的iOS应用程序: 我要做的是遍历该部分,并在UITableView中为该匹配中心数组中的每个对象创建一个新部分 在本例中,数组中有三个对象,每个对象都包含一个Top3NSDictionary,其值是一个由3个项组成的数组,每个项都是另一个属性数组。如您所见,我希望将其设置为每个部分有3个单元格,分别对应于相应matchCenterArray对象的前3项中的每一项。然后,我希望它提取每个项目的属性,并在每个单元格中显示为texLabel、detail

我的解析云代码使用以下结构将JSON发送回我的iOS应用程序:

我要做的是遍历该部分,并在UITableView中为该
匹配中心数组中的每个对象创建一个新部分

在本例中,数组中有三个对象,每个对象都包含一个
Top3
NSDictionary,其值是一个由3个项组成的数组,每个项都是另一个属性数组。如您所见,我希望将其设置为每个部分有3个单元格,分别对应于相应
matchCenterArray
对象的前3项中的每一项。然后,我希望它提取每个项目的属性,并在每个单元格中显示为
texLabel
detailtextblabel
和缩略图

我曾尝试使用for循环作为解决方案,但这会在数组的所有单元格中显示相同的项。这可能是因为我只循环使用matchCenterArray对象,而没有额外循环使用这些对象中的项,如图所示:

cell.textLabel.text = [[[[_matchCenterArray  objectAtIndex:i] objectForKey:@"Top 3"] objectAtIndex:0]objectForKey:@"Title"];
我想做的是在这个循环中嵌套一个for循环,代替
objectAtIndex:0
,但是向for循环发送消息不起作用

MatchCenterViewController.m:

#import "MatchCenterViewController.h"
#import <UIKit/UIKit.h>

@interface MatchCenterViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *matchCenter;
@end

@implementation MatchCenterViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.matchCenter = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewCellStyleSubtitle];
    self.matchCenter.frame = CGRectMake(0,50,320,self.view.frame.size.height-100);
    _matchCenter.dataSource = self;
    _matchCenter.delegate = self;
    [self.view addSubview:self.matchCenter];

    _matchCenterArray = [[NSArray alloc] init];
}

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

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

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

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


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

//the part where i setup sections and the deleting of said sections

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 21.0f;
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 21)];
    headerView.backgroundColor = [UIColor lightGrayColor];

//    _searchTerm = [[self.matchCenterArray firstObject] objectForKey:@"Search Term"];

    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, 250, 21)];
//    headerLabel.text = [NSString stringWithFormat:@"%@", searchTerm];
//    headerLabel.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
//    headerLabel.textColor = [UIColor whiteColor];
    headerLabel.backgroundColor = [UIColor lightGrayColor];
    [headerView addSubview:headerLabel];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = section + 1000;
    button.frame = CGRectMake(300, 2, 17, 17);
    [button setImage:[UIImage imageNamed:@"xbutton.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:button];
    return headerView;
}



- (IBAction)deleteButtonPressed:(UIButton *)sender {
    NSLog(@"Search Term: '%@'", _searchTerm);

    [PFCloud callFunctionInBackground:@"deleteFromMatchCenter"
                       withParameters:@{
                                        @"searchTerm": _searchTerm,
                                       }
                                block:^(NSDictionary *result, NSError *error) {

                                    if (!error) {
                                        NSLog(@"Result: '%@'", result);
                                    }
                                }];
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return 3;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Initialize cell
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        // if no cell could be dequeued create a new one
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }






    for (int i = 0; i<[_matchCenterArray count]; i++) {


    // populate dictionary with results

    //NSDictionary *matchCenterDictionary= [_matchCenterArray objectAtIndex:indexPath.row];

    // title of the item
    cell.textLabel.text = [[[[_matchCenterArray  objectAtIndex:i] objectForKey:@"Top 3"] objectAtIndex:0]objectForKey:@"Title"];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:12];

    // price of the item
    cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@", [[[[_matchCenterArray  objectAtIndex:i] objectForKey:@"Top 3"] objectAtIndex:0]objectForKey:@"Price"]];
    cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];

    // image of the item
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[[_matchCenterArray  objectAtIndex:i] objectForKey:@"Top 3"] objectAtIndex:0] objectForKey:@"Image URL"]]];
    [[cell imageView] setImage:[UIImage imageWithData:imageData]];
    //imageView.frame = CGRectMake(45.0,10.0,10,10);



    }

    return cell;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
#导入“MatchCenterViewController.h”
#进口
@接口匹配CenterViewController()
@属性(非原子,强)UITableView*matchCenter;
@结束
@CenterViewController的实现
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
self.matchCenter=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewCellStyleSubtitle];
self.matchCenter.frame=CGRectMake(0,50320,self.view.frame.size.height-100);
_matchCenter.dataSource=self;
_matchCenter.delegate=self;
[self.view addSubview:self.matchCenter];
_matchCenterArray=[[NSArray alloc]init];
}
-(无效)视图显示:(BOOL)动画
{
self.matchCenterArray=[[NSArray alloc]init];
[PFCloud callFunctionInBackground:@“MatchCenter测试”
带参数:@{
@“测试”:@“嗨”,
}
块:^(NSArray*结果,NSError*错误){
如果(!错误){
_matchCenterArray=结果;
[_matchCenter重新加载数据];
NSLog(@“结果:'%@'”,结果);
}
}];
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回_matchCenterArray.count;
}
//我设置节和删除所述节的部分
-(CGFloat)表格视图:(UITableView*)表格视图头部高度部分:(NSInteger)部分{
返回21.0f;
}
-(UIView*)表格视图:(UITableView*)表格视图用于标题部分:(NSInteger)部分{
UIView*headerView=[[UIView alloc]initWithFrame:CGRectMake(0,0320,21)];
headerView.backgroundColor=[UIColor lightGrayColor];
//_searchTerm=[[self.matchCenterArray firstObject]objectForKey:@“搜索项”];
UILabel*headerLabel=[[UILabel alloc]initWithFrame:CGRectMake(8,0,250,21)];
//headerLabel.text=[NSString stringWithFormat:@“%@”,searchTerm];
//headerLabel.font=[UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
//headerLabel.textColor=[UIColor whiteColor];
headerLabel.backgroundColor=[UIColor lightGrayColor];
[headerView添加子视图:headerLabel];
UIButton*button=[UIButton button类型:UIButtonTypeCustom];
button.tag=区段+1000;
button.frame=CGRectMake(300,2,17,17);
[按钮设置图像:[UIImage ImageName:@“xbutton.png”]用于状态:UIControlStateNormal];
[按钮添加目标:自我操作:@选择器(deleteButtonPressed:)for ControlEvents:UIControlEventTouchUpInside];
[头视图添加子视图:按钮];
返回headerView;
}
-(iAction)DeleteButton按下:(UIButton*)发送方{
NSLog(@“搜索词:'%@'”,_searchTerm);
[PFCloud callFunctionInBackground:@“deleteFromMatchCenter”
带参数:@{
@“searchTerm”:\u searchTerm,
}
块:^(NSDictionary*结果,NSError*错误){
如果(!错误){
NSLog(@“结果:'%@'”,结果);
}
}];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回3;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
//初始化单元格
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(!单元格){
//如果没有单元格可以退出队列,请创建一个新的单元格
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier];
}

for(int i=0;i
cellforrow索引路径:
在节:

您还需要添加
numberOfSections

应该是这样的:

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Initialize cell
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        // if no cell could be dequeued create a new one
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // the following will set the cell attributes for cells in each section
    // to customize per section, use indexPath.section to get which section you are at

    // title of the item
    cell.textLabel.text = [[[[_matchCenterArray  objectAtIndex:indexPath.row objectForKey:@"Top 3"] objectAtIndex:0]objectForKey:@"Title"];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:12];

    // price of the item
    cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@", [[[[_matchCenterArray  objectAtIndex:indexPath.row] objectForKey:@"Top 3"] objectAtIndex:0]objectForKey:@"Price"]];
    cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];

    // image of the item
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[[_matchCenterArray  objectAtIndex:indexPath.row] objectForKey:@"Top 3"] objectAtIndex:0] objectForKey:@"Image URL"]]];
    [[cell imageView] setImage:[UIImage imageWithData:imageData]];

    return cell;
}

请参阅

忘记循环,委托方法已在迭代次数等于数据源计数的循环上运行

NumberOfSectionsTableView

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

- (NSInteger)tableView:(UITableView *)tableView 
                                   numberOfRowsInSection:(NSInteger)section{
     return _matchCenterArray[section][@"Top 3"].count;
}
cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView 
                           cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // Initialize cell
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        // if no cell could be dequeued create a new one
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                                         reuseIdentifier:CellIdentifier];
    }

    // title of the item
    cell.textLabel.text = _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row][@"Title"];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:12];

    // price of the item
    cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@", _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row][@"Price"];
    cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];

    // image of the item
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][@"Top 3"][indexPath.row][@"Image URL"]]];
    [[cell imageView] setImage:[UIImage imageWithData:imageData]];

    return cell;

}
  • 不要对数组计数进行harcode,如果json发生更改,则需要更改代码
  • 将来你应该上厕所