gpt4 book ai didi

objective-c - iOS UITableView 滚动时只重新加载单元格数据

转载 作者:行者123 更新时间:2023-11-28 19:19:51 25 4
gpt4 key购买 nike

我正在为 iOS 开发我的第一个 Objective-C 应用程序,但在重新加载 UITableView 中的数据时遇到了问题。

重新加载数据后,只有当单元格滚动到容器可视区域上方或下方时,单元格内容才会更新。

这是我的 .h 代码:

#import <UIKit/UIKit.h>
#import "AFHTTPClient.h"
#import "AFJSONRequestOperation.h"

@interface HelloWorldViewController : UIViewController <UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource>{
NSMutableArray *tableViewArray;
IBOutlet UITableView *tableView;
}
@property (nonatomic, retain) NSMutableArray *tableViewArray;
@property (weak, nonatomic) IBOutlet UILabel *connectionLabel;
@property (nonatomic, retain) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UITextView *textArea;
@property (weak, nonatomic) IBOutlet UITextField *textField2;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (copy, nonatomic) NSString *userName;
@property (copy, nonatomic) NSString *passWord;
@property (copy, nonatomic) NSMutableString *serverResponse;
- (IBAction)callHome:(id)sender;
@end

和.m代码:

#import "HelloWorldViewController.h"

@interface HelloWorldViewController ()

@end

@implementation HelloWorldViewController
@synthesize tableViewArray;
@synthesize connectionLabel;
@synthesize userName = _userName;
@synthesize passWord = _password;
@synthesize serverResponse = _serverResponse;
@synthesize tableView;
@synthesize textArea;
@synthesize textField2;
@synthesize label;
@synthesize textField;

- (void)viewDidLoad
{
[super viewDidLoad];
tableViewArray = [[NSMutableArray alloc] init];
[tableViewArray addObject:@"TEST1"];
[tableViewArray addObject:@"TEST2"];
[tableViewArray addObject:@"TEST3"];
}

- (void)viewDidUnload
{
[self setTextField:nil];
[self setLabel:nil];
[self setTextField2:nil];
[self setTextArea:nil];
[self setTableView:nil];
[self setConnectionLabel:nil];
[super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == self.textField) {
[theTextField resignFirstResponder];
}
return YES;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

cell.textLabel.text = [self.tableViewArray objectAtIndex: [indexPath row]];
return cell;
}

- (IBAction)callHome:(id)sender {
self.userName = self.textField.text;
self.passWord = self.textField2.text;

NSMutableString *tempResponse = [[NSMutableString alloc] initWithFormat:@""];

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://example.com/"]];

[client setAuthorizationHeaderWithUsername:self.userName password:self.passWord];

[client getPath:@"login.do" parameters:nil
success:^( AFHTTPRequestOperation *operation , id responseObject ){
NSLog(@"Authentication Success: %d", operation.response.statusCode);
self.serverResponse = [NSMutableString stringWithFormat:@"Authentication Success: %d", operation.response.statusCode ];
[tempResponse appendString: self.serverResponse];
self.textArea.text = tempResponse;
}
failure:^(AFHTTPRequestOperation *operation , NSError *error){
NSLog(@"Authentication Error: %@\n%@", error, operation);
}
];

[client getPath:@"test.json.do" parameters:nil
success:^( AFHTTPRequestOperation *operation , id responseObject ){
NSLog(@"Retrieval Success: %d", operation.response.statusCode);
NSDictionary *results = [operation.responseString JSONValue];
NSMutableArray *buildings = [results objectForKey:@"buildings"];
NSMutableArray *names = [[NSMutableArray alloc] init];
for (NSDictionary *building in buildings)
{
[names addObject:[building objectForKey:@"name"]];
}
self.tableViewArray = names;
self.serverResponse = [NSMutableString stringWithFormat:@"\nBuilding List Retrieval Success: %d", operation.response.statusCode ];
[tempResponse appendString: self.serverResponse];
self.connectionLabel.text = tempResponse;
}
failure:^(AFHTTPRequestOperation *operation , NSError *error){
NSLog(@"Retrieval Error: %@\n%@", error, operation);
}
];

NSLog(@"tableView is: %@", [tableView description]);
[tableView reloadData];
}

@end

当我调用 [self.tableView description] 时,结果为空,但如果我从 cellForRowAtIndexPath 调用它,则会得到以下结果:

tableView is: <UITableView: 0x8a71000; frame = (0 0; 280 191); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x6b7e860>; contentOffset: {0, 0}>. Delegate: HelloWorldViewController, DataSource: HelloWorldViewController

这是界面生成器的屏幕截图: enter image description here

感谢所有帮助!谢谢!

最佳答案

您可能没有在界面构建器中连接 UITableView..

您必须在按住 ctrl 的同时从文件所有者拖动到 UITableView 并连接它。

另外,你不应该在没有 self 的情况下访问你的属性,你应该这样做:

@synthesize tableViewArray = _tableViewArray;

然后访问它:

self.tableViewArray

尽量避免直接访问您的 ivars,使用该属性!

祝你好运!

关于objective-c - iOS UITableView 滚动时只重新加载单元格数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9846882/

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