gpt4 book ai didi

ios - Objective-C : My UITableView doesn't see new data

转载 作者:行者123 更新时间:2023-11-29 02:21:01 25 4
gpt4 key购买 nike

我有我的 TableView 的数据源,它很难处理 block 和其他东西。我完全搞砸了(((请帮帮我,我做错了什么?

我的数据源代码:

@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 个对象的数组;并且在我之后只有 2 个对象返回 [[[BGMDataStorage sharedDataStorage] dataItems] count]。

最佳答案

在重新加载 TableView 之前没有更新数据源(dataItems 数组),应该在调用[tableView reloadData] 之前更新数据源。否则旧数据将被加载到表中。

试试这个,

在 TableView Controller 中的 viewDidLoad 方法中编写此代码

- (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);

}

...

关于ios - Objective-C : My UITableView doesn't see new data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28135797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com