我正在构建一个 iOS 应用程序,它有一个包含多个部分的 TableView 。
部分取决于 NSMutableDictionary 上的键数。
每次到达该行的最后一个单元格时,我都试图通过向该字典添加数据来进行分页。
我的问题是分页事件被多次触发,使各部分看起来顺序不同。我想最后一个问题是因为我用来添加新数据的方法是 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
TableView Controller :
@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
最佳答案
字典是无序的集合。每次表格 View 请求信息时,表格 View 中的部分和行都必须以相同的顺序提供,因此字典不适合。
对于单节 TableView 使用单个 NSArray 或 NSMutableArray 或对于多节 TableView 使用数组数组是很常见的。 (外部数组包含您的部分,内部数组包含每个部分的行。
如果有帮助,您可以将每个单元格的数据设为一个字典(这样您就会有一个字典数组的数组。)这样,内部字典中的每个键/值对都包含您需要在您的列表中显示的不同信息细胞。
我建议不要使用字典来表示表格 View 中的单元格。 (但如前所述,您可以使用字典来保存单元格的不同设置。)
关于iOS - 使用基于字典的 UITableView 进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32660273/