【问题标题】:iOS - Pagination using a Dictionary based UITableViewiOS - 使用基于字典的 UITableView 进行分页
【发布时间】:2015-12-16 02:10:00
【问题描述】:

我正在构建一个 iOS 应用程序,它有一个包含多个部分的 tableview。

这些部分取决于 NSMutableDictionary 上的键数。

我试图通过每次到达该行的最后一个单元格时向该字典添加数据来进行分页。

我的问题是分页事件被多次触发,并使部分看起来以不同的顺序。我猜最后一个问题是因为我用来添加新数据的方法是addEntriesFromDictionary

所以基本上我的问题是: - Dictionary 数据源方法是否合适?我应该为数组数据源更改它吗? - 多次分页调用问题的原因

到目前为止,这是我的代码:

@implementation FooViewController

- (void)viewDidLoad {

    self.FooStore = [[FooStore alloc]init];
    [self.FooStore getFooDTO: @1];
    [self setNavigationBar:nil];
    [self setNeedsStatusBarAppearanceUpdate];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleFooDTOChange:)
                                                 name:@"FooDTOChanged"
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleLoadDataFoo:)
                                                 name:@"loadDataFoo"
                                               object:nil];

    self.activityView = [[UIActivityIndicatorView alloc]
                                             initWithActivityIndicatorStyle:    UIActivityIndicatorViewStyleGray];


    self.activityView.center = self.view.center;
    [self.activityView startAnimating];
    [self.view addSubview:self.activityView];

}


- (void)handleFooDTOChange:(NSNotification *)note {
    NSDictionary *theData = [note userInfo];
    if (theData != nil) {
        FooDTO *FooDTO = theData[@"FooDTO"];
        if(FooDTO.date != nil){
            [self setNavigationBar:FooDTO.date];
            [self.activityView stopAnimating];
        }

    }
}

- (void)handleLoadDataFoo:(NSNotification *)note {
    NSDictionary *theData = [note userInfo];
    if (theData != nil) {
        [self.FooStore getFooDTO: theData[@"paginaActual"]];
    }
}

… some code ..


@end

表格视图控制器:

@implementation FooTableViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentPage = 1;
    self.data = [[NSMutableDictionary alloc] init];
    [self.tableView setHidden:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleFooDTOChange:)
                                                 name:@"FooDTOChanged"
                                               object:nil];


}


- (void)handleFooDTOChange:(NSNotification *)note {
    NSDictionary *theData = [note userInfo];
    if (theData != nil) {
        FooDTO *FooDTO = theData[@"FooDTO"];
        if ([[FooDTO.dataFoo allKeys] count] > 0){
            [self.data addEntriesFromDictionary: FooDTO.dataFoo];
            [self.tableView setHidden:NO];
            [self.tableView reloadData];
        }
    }
}

-(void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return self.data.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    NSString *arrayKey = [self.data.allKeys objectAtIndex:section];
    NSArray *a = [self.data objectForKey:arrayKey];

    return a.count;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *arrayKey = [self.data.allKeys objectAtIndex:indexPath.section];
    NSArray *dataKey = [self.data objectForKey:arrayKey];

    if(indexPath.row == [dataKey count] -1 && indexPath.section == [self.data.allKeys count] - 1){
        self.currentPage++;
        NSDictionary *dataDictionary = @{
                                         @"currentPage": [NSNumber numberWithInt: self.currentPage]
                                        };
        [[NSNotificationCenter defaultCenter] postNotificationName:@"loaddataFoo" object:self userInfo:dataDictionary];
    }
}

服务:

@implementation FooStore

- (FooDTO *) getFooDTO: (NSNumber *) page
{

    FooDTO *FooDTO =[[FooDTO alloc]init];


    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    NSString *dateStr = [dateFormat stringFromDate:date];
    NSDictionary *parameters = @{
                //urlparameters
                                 };

    [manager POST:@"http://my.service/foos.json" parameters:parameters
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //handle data
              }


              NSDictionary *dataDictionary = @{
                                               @"FooDTO": FooDTO
                                               };

              [[NSNotificationCenter defaultCenter] postNotificationName:@"FooDTOChanged" object:self userInfo:dataDictionary];
          } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              NSLog(@"Error: %@", error);
              NSDictionary *dataDictionary = @{
                                               @"error": error
                                               };


              [[NSNotificationCenter defaultCenter] postNotificationName:@"FooError" object:self userInfo:dataDictionary];
          }
    ];

    return nil;
};

@end

【问题讨论】:

    标签: ios objective-c uitableview dictionary pagination


    【解决方案1】:

    字典是无序的集合。每次表格视图请求信息时,表格视图中的部分和行必须以相同的顺序提供,因此字典不适合。

    将单个 NSArray 或 NSMutableArray 用于单节表视图或将数组数组用于多节表视图是很常见的。 (外部数组包含您的部分,内部数组包含每个部分的行。

    如果有帮助,您可以将每个单元格的数据设为字典(因此您将拥有一个字典数组。)这样,内部字典中的每个键/值对都包含您需要在您的细胞。

    我建议不要使用字典来表示表格视图中的单元格。 (但如前所述,您可以使用字典来保存单元格的不同设置。)

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 2018-05-02
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 2023-02-23
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多