gpt4 book ai didi

ios - 重新加载 tableView - Objective C

转载 作者:行者123 更新时间:2023-11-28 22:11:41 24 4
gpt4 key购买 nike

调用[self.tableView reloadData];时我的cellForRowAtIndexPath不会运行。

这是正在发生的事情:

第一个序列 -

- (void)startProcess:(NSInteger)number {
NSInteger testing = number;
cellID = testing;
// MAKE REQuEST TO SERVER
[self makeRequests];

}

第二序列-

-(void)makeRequests
{
/* GRAB USERNAME TO BE SENT OVER FOR NOTIFICATIONS */
NSArray *get = [[SSKeychain allAccounts] init];
NSString *username = [get[0] objectForKey:@"acct"];

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:@"mySite.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"];
NSLog(@"grabbing data");
[self.tableView reloadData];
}

现在这将导致我的 numberOfRowsInSection可以正常运行和工作,但是 cellForRowAtIndexPath从不运行。这是我的 numberOfRowsInSection :

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

impactsGrabed count将返回例如 2。

tableView 的输出:

<UITableView: 0x10e00ee00; frame = (0 20; 320 548); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x10c244950>; layer = <CALayer: 0x10c23ffc0>; contentOffset: {0, 0}>

更新:

单元格表代码:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.impactsGrabed count]; <!-- this does return an positive integer.
}

- (double) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 75;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"working cell");
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;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// WHEN CLICKING ON A NOTIFICATION FOR THE PAGE

}

这是我注意到的:

调用函数时startProcess来自另一个开始加载 tableView cells 的整个过程的类这是行不通的。如果我调用 startProcess viewDidLoad 中的方法在该文件中,启动时 tableView cells做负载。

最佳答案

如果 numberOfRowsInSection 返回零,则不会调用 cellForRowAtIndexPath。只需通过调试代码确保 numberOfRowsInSection 返回非零值即可。

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

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