2012-03-23 127 views
0

我正在爲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 

下面是界面生成器的屏幕截圖: enter image description here

所有幫助表示讚賞!謝謝!

+0

我添加了界面構建器的屏幕截圖。我ctrl從突出顯示的表視圖拖動到Hello World View控制器。再次感謝... – kevinstueber 2012-03-24 02:09:06

回答

4

你可能不會在界面生成器連接的UITableView ..

你必須同時按下從文件的所有者CTRL到的UITableView拖動和連接。

而且,你不能沒有自我訪問你的財產,你應該做的:

@synthesize tableViewArray = _tableViewArray; 

,然後用訪問:

self.tableViewArray 

儘量避免訪問您的高德直接使用屬性!

祝你好運!

+0

感謝您的回覆Heliem。我爲這個問題添加了一個截圖。也許我會拖到錯誤的地方? – kevinstueber 2012-03-24 02:09:55

+0

你必須反過來拖動,從視圖控制器到高亮表格視圖 – Zalykr 2012-03-24 02:13:48

+0

哈! Xcode有時候太有趣了!就是這樣。謝謝你的幫助! – kevinstueber 2012-03-24 02:19:57

0

看起來你可能沒有在IB中使用HelloWorldViewController的tabelView屬性來連接你的UITableView。

+0

大家好,彼得,謝謝你的幫助。我爲該問題添加了我的界面生成器的屏幕截圖。請讓我知道是否有什麼不合適的地方。 – kevinstueber 2012-03-24 02:10:45

相關問題