UITableView неправильно заполняет разделы содержимым NSArray

#ios #objective-c #arrays #json #uitableview

#iOS #objective-c #массивы #json #uitableview uitableview

Вопрос:

Мой код Parse cloud отправляет обратно JSON в мое приложение для iOS со следующей структурой:

введите описание изображения здесь

То, что я хочу сделать, это повторить это и создать новый раздел в UITableView для каждого объекта в этом matchCenterArray .

В этом случае в массиве есть три объекта, каждый из которых содержит Top 3 NSDictionary, значение которого представляет собой массив из 3 элементов, каждый из которых является еще одним массивом свойств. Как вы можете видеть, я хочу, чтобы он был настроен так, чтобы в каждом разделе было 3 ячейки, по одной для каждого из 3 верхних элементов соответствующего matchCenterArray объекта. Затем я хочу, чтобы он извлекал свойства каждого элемента и отображал его в каждой ячейке как texLabel , detailTextLabel , и эскиз.

Я пытался использовать цикл 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 = resu<
                                        [_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
 

Комментарии:

1. Используете ли вы раскадровку? Кроме того, в cellForRowAtIndexPath не похоже, что вы снова выполняете итерацию через matchCenterArray. Разве это не должен быть другой массив? matchCenterArray — это то, что вы используете для создания разделов. Возможно, именно поэтому он показывает один и тот же объект

2. Я использую раскадровку в целом, но не для создания UITableView , что делается программно. Да, это то, что я не могу понять, я думал о том, чтобы поместить туда еще один цикл for , но я не уверен, как написать синтаксис, который отправил бы сообщение в цикл for .

3. что значит отправить сообщение в цикл for? Разве вы не можете просто перебирать массив в своем словаре? Если вы можете, обновите свой ответ немного подробнее.

4. Я обновил свой вопрос, чтобы объяснить, что я имею в виду.

5. Я все еще не понимаю, что такое отправить сообщение в цикл for. Я бы разделил все на части для каждой ячейки. Поэтому создайте новый экземпляр словаря для каждой позиции в _matchCenterArray, а затем создайте новый массив, который соответствует ключу в словаре для того, что вы хотите, а затем загрузите значения меток из нового массива. В каждой ячейке у вас будут новые объекты, которые упростят ваш обход. Вы можете вложить циклы for и просто выделить ячейку во внутреннем самом цикле.

Ответ №1:

Забудьте о циклах, метод делегирования уже выполняется в цикле, где номер итерации равен количеству источников данных.

numberOfSectionsInTableView

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

numberOfRowsInSection

 - (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;

}
 
  • Не кодируйте количество массивов, если ваш json изменится, ваш код нужно будет изменить.
  • В будущем вам следует изучить возможность асинхронной загрузки изображений с использованием библиотеки, такой как SDWebImage, чтобы избежать задержек.

Ответ №2:

cellForRowAtIndexPath: вызывается один раз за numberOfRowsInSection:

Вам 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;
}
 

Пожалуйста, обратитесь к руководству Apple по просмотру таблиц