iOS-使用基于词典的UITableView进行分页
我正在构建一个iOS应用程序,它有一个包含多个部分的tableview 节数取决于NSMutableDictionary上的键数 我试图通过在每次到达行的最后一个单元格时向字典添加数据来进行分页 我的问题是分页事件被多次触发,并使各部分看起来顺序不同。我想最后一个问题是因为我用来添加新数据的方法是iOS-使用基于词典的UITableView进行分页,ios,objective-c,uitableview,dictionary,pagination,Ios,Objective C,Uitableview,Dictionary,Pagination,我正在构建一个iOS应用程序,它有一个包含多个部分的tableview 节数取决于NSMutableDictionary上的键数 我试图通过在每次到达行的最后一个单元格时向字典添加数据来进行分页 我的问题是分页事件被多次触发,并使各部分看起来顺序不同。我想最后一个问题是因为我用来添加新数据的方法是addEntriesFromDictionary 所以基本上我的问题是: -字典数据源方法正确吗?我应该将其更改为数组数据源吗? -多重分页调用问题的原因 以下是我目前的代码: @implementat
addEntriesFromDictionary
所以基本上我的问题是:
-字典数据源方法正确吗?我应该将其更改为数组数据源吗?
-多重分页调用问题的原因
以下是我目前的代码:
@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
字典是无序的集合。每次表视图请求信息时,表视图中的节和行必须以相同的顺序提供,因此字典不适合 对于单个分区表视图使用单个NSArray或NSMutableArray,或者对于多分区表视图使用数组数组,这是非常常见的。(外部数组包含节,内部数组包含每个节的行 如果有帮助的话,可以将每个单元格的数据设置为字典(这样就可以有一个字典数组)。这样,内部字典中的每个键/值对都包含需要在单元格中显示的不同信息
我建议不要使用字典来表示表视图中的单元格(但如上所述,您可以使用字典来保存单元格的不同设置)词典是无序的集合。每次表视图要求信息时,表视图中的节和行必须以相同的顺序提供,因此词典不适用 对于单节表视图,使用单个NSArray或NSMutableArray,或者对于多节表视图,使用数组数组数组是非常常见的。(外部数组包含节,内部数组包含每个节的行。) 如果有帮助的话,可以将每个单元格的数据设置为字典(这样就可以有一个字典数组)。这样,内部字典中的每个键/值对都包含需要在单元格中显示的不同信息
我建议不要使用字典来表示表视图中的单元格(但如上所述,您可以使用字典来保存单元格的不同设置)词典是无序的集合。每次表视图要求信息时,表视图中的节和行必须以相同的顺序提供,因此词典不适用 对于单节表视图,使用单个NSArray或NSMutableArray,或者对于多节表视图,使用数组数组数组是非常常见的。(外部数组包含节,内部数组包含每个节的行。) 如果有帮助的话,可以将每个单元格的数据设置为字典(这样就可以有一个字典数组)。这样,内部字典中的每个键/值对都包含需要在单元格中显示的不同信息
我建议不要使用字典来表示表视图中的单元格(但如上所述,您可以使用字典来保存单元格的不同设置)词典是无序的集合。每次表视图要求信息时,表视图中的节和行必须以相同的顺序提供,因此词典不适用 对于单节表视图,使用单个NSArray或NSMutableArray,或者对于多节表视图,使用数组数组数组是非常常见的。(外部数组包含节,内部数组包含每个节的行。) 如果有帮助的话,可以将每个单元格的数据设置为字典(这样就可以有一个字典数组)。这样,内部字典中的每个键/值对都包含需要在单元格中显示的不同信息 我建议不要使用字典来表示表视图中的单元格(但如上所述,您可以使用字典来保存单元格的不同设置)