【发布时间】: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