Objective C: 我的 UITableView 没有看到新数据
Objective C: My UITableView doesn't see new data
我有我的 TableView 的数据源,它很难处理块和其他东西。我完全搞砸了(((请帮帮我,我做错了什么?
我的数据源代码:
@synthesize dataItems;
+ (instancetype)sharedDataStorage
{
static BGMDataStorage *sharedDataStorage = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedDataStorage = [[self alloc] initPrivate];
});
return sharedDataStorage;
}
- (instancetype)initPrivate
{
self = [super init];
if (self)
{
// !!!! TEST ARRAY
NSDictionary *preLoadDict = @{
@"Val 1" : @"34",
@"Val 2" : @"69",
};
dataItems = [[NSArray alloc] initWithObjects:preLoadDict, nil];
}
return self;
}
- (void)buildDataSourcesFor:(NSString *)soapMethodName
OnDate:(NSDate *)date
WithParams:(NSString *)param
WithCompletion:(void (^)(NSString *))compblock
failure:(void (^)(NSError *error))failure
{
[[BGMSbrDataProvider alloc] callWebServiceFor:soapMethodName
OnDate:date
WithSuccess:^(NSDictionary *resultDict) {
//success block
dataItems = [resultDict objectForKey:param];
if (dataItems)
{
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataItemsArrayWasBuilt"
object:nil];
});
}
NSString *stringData = [self chartDataJsonStringFromArray:dataItems
forMethodName:soapMethodName];
compblock(stringData);
}
...
}
UITableView代码没有什么特别之处:
//get the number of rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//return the number of rows in the section
return [[[BGMDataStorage sharedDataStorage] dataItems] count];
}
//configure the cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//our cell
//set the CellIdentifier depends on amount of columns
NSArray *allKeys = [[[[BGMDataStorage sharedDataStorage] dataItems] objectAtIndex:0] allKeys];
NSUInteger myUnitKeysCounter = [allKeys count];
NSString *theCellIdentifier = [NSString stringWithFormat:@"ListPrototypeCell%hu", (unsigned short)myUnitKeysCounter];
DDLogVerbose(@"The current CellIdentifier is: %@.", theCellIdentifier);
BGMDataTableViewCell *theCell = [self.tableView dequeueReusableCellWithIdentifier:theCellIdentifier forIndexPath:indexPath];
//configure the cell...
if ([theCellIdentifier isEqualToString:@"ListPrototypeCell1"])
{
theCell.labelText1.text = [[[[BGMDataStorage sharedDataStorage] dataItems] objectAtIndex:indexPath.row] objectForKey:[allKeys objectAtIndex:0]];
}
else if ([theCellIdentifier isEqualToString:@"ListPrototypeCell2"])
{
theCell.labelText1.text = [[[[BGMDataStorage sharedDataStorage] dataItems] objectAtIndex:indexPath.row] objectForKey:[allKeys objectAtIndex:0]];
theCell.labelText2.text = [[[[BGMDataStorage sharedDataStorage] dataItems] objectAtIndex:indexPath.row] objectForKey:[allKeys objectAtIndex:1]];
}
我在调试中看到在 buildDataSourcesFor: 时我在 dataItems 中有数据,但在此之后我在 uitableview 中只看到 TEST ARRAY 数据。我想也许我迷路了,但无论如何我走得太远了。
所以我的问题是我在 dataItems = [resultDict objectForKey:param]; 的断点处看到有 24 个对象的数组;并且在我之后 return [[[BGMDataStorage sharedDataStorage] dataItems] count] 只有 2 个对象。
您在重新加载 table 视图之前没有更新数据源(dataItems 数组),应该在调用 [tableView reloadData]
之前更新数据源。否则旧数据将被加载到 table.
试试这个,
将这段代码写在 viewDidLoad
方法中 table 视图控制器
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveDataItemsArrayWasBuiltNotification:) name:@"DataItemsArrayWasBuilt" object:nil];
}
- (void)receiveDataItemsArrayWasBuiltNotification:(NSNotification *)notification
{
//if ([[notification name] isEqualToString:@"DataItemsArrayWasBuilt"]){
[self.tableView reloadData];
//}
}
- (void)buildDataSourcesFor:(NSString *)soapMethodName
OnDate:(NSDate *)date
WithParams:(NSString *)param
WithCompletion:(void (^)(NSString *))compblock
failure:(void (^)(NSError *error))failure
{
[[BGMSbrDataProvider alloc] callWebServiceFor:soapMethodName
OnDate:date
WithSuccess:^(NSDictionary *resultDict) {
//success block
dataItems = [resultDict objectForKey:param];
if (dataItems)
{
sharedDataStorage.dataItems = dataItems;// You missed this line
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataItemsArrayWasBuilt"
object:nil];
});
}
NSString *stringData = [self chartDataJsonStringFromArray:dataItems
forMethodName:soapMethodName];
compblock(stringData);
}
...
}
我有我的 TableView 的数据源,它很难处理块和其他东西。我完全搞砸了(((请帮帮我,我做错了什么?
我的数据源代码:
@synthesize dataItems;
+ (instancetype)sharedDataStorage
{
static BGMDataStorage *sharedDataStorage = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedDataStorage = [[self alloc] initPrivate];
});
return sharedDataStorage;
}
- (instancetype)initPrivate
{
self = [super init];
if (self)
{
// !!!! TEST ARRAY
NSDictionary *preLoadDict = @{
@"Val 1" : @"34",
@"Val 2" : @"69",
};
dataItems = [[NSArray alloc] initWithObjects:preLoadDict, nil];
}
return self;
}
- (void)buildDataSourcesFor:(NSString *)soapMethodName
OnDate:(NSDate *)date
WithParams:(NSString *)param
WithCompletion:(void (^)(NSString *))compblock
failure:(void (^)(NSError *error))failure
{
[[BGMSbrDataProvider alloc] callWebServiceFor:soapMethodName
OnDate:date
WithSuccess:^(NSDictionary *resultDict) {
//success block
dataItems = [resultDict objectForKey:param];
if (dataItems)
{
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataItemsArrayWasBuilt"
object:nil];
});
}
NSString *stringData = [self chartDataJsonStringFromArray:dataItems
forMethodName:soapMethodName];
compblock(stringData);
}
...
}
UITableView代码没有什么特别之处:
//get the number of rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//return the number of rows in the section
return [[[BGMDataStorage sharedDataStorage] dataItems] count];
}
//configure the cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//our cell
//set the CellIdentifier depends on amount of columns
NSArray *allKeys = [[[[BGMDataStorage sharedDataStorage] dataItems] objectAtIndex:0] allKeys];
NSUInteger myUnitKeysCounter = [allKeys count];
NSString *theCellIdentifier = [NSString stringWithFormat:@"ListPrototypeCell%hu", (unsigned short)myUnitKeysCounter];
DDLogVerbose(@"The current CellIdentifier is: %@.", theCellIdentifier);
BGMDataTableViewCell *theCell = [self.tableView dequeueReusableCellWithIdentifier:theCellIdentifier forIndexPath:indexPath];
//configure the cell...
if ([theCellIdentifier isEqualToString:@"ListPrototypeCell1"])
{
theCell.labelText1.text = [[[[BGMDataStorage sharedDataStorage] dataItems] objectAtIndex:indexPath.row] objectForKey:[allKeys objectAtIndex:0]];
}
else if ([theCellIdentifier isEqualToString:@"ListPrototypeCell2"])
{
theCell.labelText1.text = [[[[BGMDataStorage sharedDataStorage] dataItems] objectAtIndex:indexPath.row] objectForKey:[allKeys objectAtIndex:0]];
theCell.labelText2.text = [[[[BGMDataStorage sharedDataStorage] dataItems] objectAtIndex:indexPath.row] objectForKey:[allKeys objectAtIndex:1]];
}
我在调试中看到在 buildDataSourcesFor: 时我在 dataItems 中有数据,但在此之后我在 uitableview 中只看到 TEST ARRAY 数据。我想也许我迷路了,但无论如何我走得太远了。 所以我的问题是我在 dataItems = [resultDict objectForKey:param]; 的断点处看到有 24 个对象的数组;并且在我之后 return [[[BGMDataStorage sharedDataStorage] dataItems] count] 只有 2 个对象。
您在重新加载 table 视图之前没有更新数据源(dataItems 数组),应该在调用 [tableView reloadData]
之前更新数据源。否则旧数据将被加载到 table.
试试这个,
将这段代码写在 viewDidLoad
方法中 table 视图控制器
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveDataItemsArrayWasBuiltNotification:) name:@"DataItemsArrayWasBuilt" object:nil];
}
- (void)receiveDataItemsArrayWasBuiltNotification:(NSNotification *)notification
{
//if ([[notification name] isEqualToString:@"DataItemsArrayWasBuilt"]){
[self.tableView reloadData];
//}
}
- (void)buildDataSourcesFor:(NSString *)soapMethodName
OnDate:(NSDate *)date
WithParams:(NSString *)param
WithCompletion:(void (^)(NSString *))compblock
failure:(void (^)(NSError *error))failure
{
[[BGMSbrDataProvider alloc] callWebServiceFor:soapMethodName
OnDate:date
WithSuccess:^(NSDictionary *resultDict) {
//success block
dataItems = [resultDict objectForKey:param];
if (dataItems)
{
sharedDataStorage.dataItems = dataItems;// You missed this line
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataItemsArrayWasBuilt"
object:nil];
});
}
NSString *stringData = [self chartDataJsonStringFromArray:dataItems
forMethodName:soapMethodName];
compblock(stringData);
}
...
}