2012-03-23 5 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コード:私は[self.tableView description]を呼び出すとき

#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 

結果はNULLですが、私は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

インタフェースビルダーのスクリーンショットを追加しました。ハイライトされたテーブルビューからHello World View Controllerにドラッグしています。もう一度ありがとう... – kevinstueber

答えて

4

あなたは、おそらくあなたは、ファイルの所有者からのUITableViewにCtrlキーを押しながらドラッグすると、それを接続する必要があります。..インターフェイスビルダーで

をのUITableViewを接続していません。

@synthesize tableViewArray = _tableViewArray; 

をし、その後でそれにアクセスします:

また、あなたが行う必要があり、自己せずにあなたのプロパティにアクセスするべきではありません

self.tableViewArray 

は、プロパティを使用し、直接あなたのアイバーズにアクセスして回避しよう!

幸運を祈る!

+0

あなたの応答ヘリームに感謝します。質問にスクリーンショットを追加しました。多分私は間違った場所に引っ張っていますか? – kevinstueber

+0

ビューコントローラーからハイライト表示されたテーブルビューに逆向きにドラッグする必要があります – Zalykr

+0

ハ! Xcodeは時々面白いです!それはそれだった。ご協力いただきありがとうございます! – kevinstueber

0

IBのHelloWorldViewController tabelViewプロパティでUITableViewを接続していないようです。

+0

こんにちはピーター、助けてくれてありがとう。質問に私のインターフェースビルダーのスクリーンショットを追加しました。何か外れていると思われる場合は教えてください。 – kevinstueber

関連する問題