iOS UITableView只滚动时重新加载单元格数据
我正在为iOS的第一个Objective-C应用程序工作,并且在重新加载UITableView中的数据时遇到问题。iOS 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
下面是界面生成器的屏幕截图:
所有帮助表示赞赏!谢谢!
你可能不会在界面生成器连接的UITableView ..
你必须同时按下从文件的所有者CTRL到的UITableView拖动和连接。
而且,你不能没有自我访问你的财产,你应该做的:
@synthesize tableViewArray = _tableViewArray;
,然后用访问:
self.tableViewArray
尽量避免访问您的高德直接使用属性!
祝你好运!
感谢您的回复Heliem。我为这个问题添加了一个截图。也许我会拖到错误的地方? – kevinstueber 2012-03-24 02:09:55
你必须反过来拖动,从视图控制器到高亮表格视图 – Zalykr 2012-03-24 02:13:48
哈! Xcode有时候太有趣了!就是这样。谢谢你的帮助! – kevinstueber 2012-03-24 02:19:57
看起来你可能没有在IB中使用HelloWorldViewController的tabelView属性来连接你的UITableView。
大家好,彼得,谢谢你的帮助。我为该问题添加了我的界面生成器的屏幕截图。请让我知道是否有什么不合适的地方。 – kevinstueber 2012-03-24 02:10:45
我添加了界面构建器的屏幕截图。我ctrl从突出显示的表视图拖动到Hello World View控制器。再次感谢... – kevinstueber 2012-03-24 02:09:06