gpt4 book ai didi

ios - 重新加载 cellForRowAtIndexPath 时出现问题 - Objective C

转载 作者:行者123 更新时间:2023-11-29 03:05:42 26 4
gpt4 key购买 nike

当从外部类调用以启动方法并重新加载 tableView 时,我在重新加载 tableView 时遇到问题

更具体地说:

下面是外部类的调用:

    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
NSInteger tag = cell.tag;
impact* myScript = [[impact alloc] init];
[myScript startProcess:tag];
}
**From here** it moves to the `impact` class and loads the `startProcess` method:

- (void)startProcess:(NSInteger)number {
NSInteger testing = number;
cellID = testing;
// MAKE REQuEST TO SERVER
[self makeRequests];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});

}

这是它调用的 makeRequests 方法:

-(void)makeRequests
{
/* GRAB USERNAME TO BE SENT OVER FOR NOTIFICATIONS */
NSArray *get = [[SSKeychain allAccounts] init];
//NSLog(@"get%@", get);
NSString *username = [get[0] objectForKey:@"acct"];
//if(cellID != 0)
//{
NSDictionary *dictionary = @{@"function": @"populateNotfications", @"username" : username};
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (error)
NSLog(@"%s: JSON encode error: %@", __FUNCTION__, error);

NSURL *url = [NSURL URLWithString:@"test.com/dev/iphone/test.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];
NSString *params = [NSString stringWithFormat:@"json=%@",
[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *paramsData = [params dataUsingEncoding:NSUTF8StringEncoding];
[request addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
[request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)[paramsData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:paramsData];


// issue the request
NSURLResponse *response = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error)
NSLog(@"%s: NSURLConnection error: %@", __FUNCTION__, error);

// GRAB STATUS OBJECT
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:returnData //1

options:kNilOptions
error:&error];
self.impactsGrabed = [json objectForKey:@"requested_data"];
//}


}

现在它将运行我的 tableView 方法,如下所示:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.impactsGrabed count];
}

- (double) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 75;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"timelineCell";
impactTimelineCell *cell = (impactTimelineCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[impactTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell initTimelineCell];

cell.statusLabel.text = [self.impactsGrabed[indexPath.row] objectForKey:@"message"];
cell.timestampLabel.text = [self.impactsGrabed[indexPath.row] objectForKey:@"time_post"];

return cell;
}

我已经记录了每种方法,并且每种方法都有效,直到我点击 cellForRowAtIndexPath 之后什么也没发生。没有什么会返回无效。现在是事情:

当从另一个类调用函数startProcess来启动加载tableView单元格的整个过程时,它不起作用。如果我在启动时调用该文件中 viewDidLoad 内的 startProcess 方法,tableView cells 会正确加载。

例如:

如果我将 [self startProcess:1]; 放在 viewDidLoad 中,它就完美了!如果我从另一个类调用 [myScript startProcess:tag];,它将无法正常工作。

你们认为问题出在哪里?

最佳答案

由于您的所有 Controller 都已实例化并显示在屏幕上,因此您不需要实例化一个(实际上您不应该实例化,因为这会创建一个不在屏幕上的新实例)。

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
NSInteger tag = cell.tag;
impact* myScript = self.parentViewController.childViewControllers[1]; // if my script is one of the child view controllers, then this is how you need to access it -- the number might be 0,1 or 2 depending on which one is the impact controller
[myScript startProcess:tag];
}

您也可以在 prepareForSegue 中执行此操作——当您的 Controller 被实例化时,所有嵌入式子 Controller 也将被实例化,您可以从 segue 的 destinationViewController 属性中获取对它们的引用。

因为这个 Controller 已经在屏幕上,所以可以在 startProcess 中调用 reloadData。

关于ios - 重新加载 cellForRowAtIndexPath 时出现问题 - Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22798644/

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