2012-03-23 7 views
0

iOS 용 내 첫 번째 Objective-C 응용 프로그램에서 작업 중이며 UITableView에서 데이터를 다시로드하는 데 문제가 있습니다.iOS UITableView는 스크롤 할 때 셀 데이터 만 다시로드합니다.

데이터를 다시로드 한 후 셀 내용이 컨테이너의 표시 가능 영역 아래로 스크롤 될 때만 업데이트됩니다. 나는 결과가 null [self.tableView description] 전화,하지만 난 cellForRowAtIndexPath에서 호출 할 경우 다음 나는를 얻을 때

#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 

:

#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 코드 : 여기

내 .H 코드 다음 결과 :

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

당신은 아마 인터페이스 빌더에서 jQuery과 연결하지 않는 ..

당신은 jQuery과에 파일의 소유자 Ctrl 키를 누른 상태에서 드래그를 연결해야합니다.

@synthesize tableViewArray = _tableViewArray; 

을 다음에 액세스 : 또한

, 당신은 자기없이 속성에 액세스 안, 당신이 할 필요가있는 property를 사용하여, 직접 인스턴스 변수에 접근하지 않도록

self.tableViewArray 

시도 !

행운을 빈다.

+0

답변 해 주셔서 감사합니다. 질문에 스크린 샷을 추가했습니다. 어쩌면 나는 잘못된 곳으로 끌고 갈거야? – kevinstueber

+0

보기 컨트롤러에서 강조 표시된 테이블보기로 다른 방향으로 드래그해야합니다. – Zalykr

+0

하! Xcode가 때때로 너무 재미 있습니다! 그거였다. 당신의 도움을 주셔서 감사합니다! – kevinstueber

0

IB의 HelloWorldViewController tabelView 속성으로 UITableView를 연결하지 않은 것 같습니다.

+0

안녕 피터, 도와 줘서 고마워. 나는 인터페이스 빌더의 스크린 샷을 질문에 추가했다. 아무 것도 보이지 않는다면 알려주세요. – kevinstueber

관련 문제