Ios 问题重新加载CellforRowatineXpath-目标C
当从外部类调用启动方法并重新加载Ios 问题重新加载CellforRowatineXpath-目标C,ios,objective-c,uitableview,Ios,Objective C,Uitableview,当从外部类调用启动方法并重新加载tableView 更具体地说: 这是来自外部班级的电话: - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
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;
}
我已经记录了每个方法,每个方法都可以工作,直到我点击cellforrowatinexpath
,在那里什么都不会发生。没有什么东西会回来。现在事情是这样的:
从另一个类调用函数startProcess
时,该函数将启动加载tableView单元格的整个过程,但该函数不起作用。如果在启动时调用该文件中的viewDidLoad
中的startProcess
方法,则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];
});
}
如果我放置[self-startProcess:1]代码>在viewDidLoad
中,它工作得非常完美!如果我调用[myscriptstartprocess:tag]代码>从另一个类,它无法正常工作
你们认为问题是什么 因为您的所有控制器都已经实例化并显示在屏幕上,所以您不需要实例化一个控制器(实际上您不应该这样做,因为这样会创建一个不在屏幕上的新实例)
您也可以在prepareForSegue中执行此操作——当您的控制器被实例化时,所有嵌入的子控制器也将被实例化,您可以从segue的destinationViewController属性获取对它们的引用
因为这个控制器已经在屏幕上,所以可以在startProcess中调用reloadData。您的问题可能是这个,impact*myScript=[[impact alloc]init];您是否已经有impact类的实例(应该是impact BTW,类名应该以大写字母开头)?如果是这样,使用alloc init只会创建一个新实例,而不是屏幕上的实例。@rdelmar这里是文件的导入#import“impact.h”
,文件的接口是impact
。这并不能回答我的问题。在didSelectItemAtIndexPath方法中调用alloc init时,您是否已经拥有影响控制器,或者您是第一次在那里创建该控制器?还有,你在哪里做的控制器?在xib或故事板中?全部在代码中?哦,@rdelmar不,我不会在任何其他地方调用alloc init
。和控制器都是在故事板中创建的。你能给我一个例子,把它放在viewdideappear
中,并把它放在哪里调用吗。@DavidBiga,不确定你要的是什么——你只需要放[self.tableView reloadData];在ViewDidDisplay:method中。我的意思是,我的其他代码是否以某种方式影响了这一点,并且需要更改,因为当我可以使用-(void)ViewDidDisplay:(BOOL)animated
方法并使用reloadData
时,它对我仍然不起作用。@DavidBiga,您是否更改了实例化控制器的方式?我将更新我的答案以给出一个例子。不,我只是将其保留在impact alloc