2013-07-22 122 views
16

我認爲我有一個非常簡單的任務,但不知何故它不想工作。我是Objective-C的初學者,所以我想這只是一個小錯誤。我仍然不知道自己在做什麼,目前它更像是複製&粘貼編程。就像我不知道我是否需要在界面中使用IBOutlet,或者作爲屬性或兩者兼而有之。刷新表格視圖?

我有什麼:

一個視圖控制器的一個按鈕,一個標籤和一個表視圖。該按鈕連接到共享點服務器並讀取列表並將該值添加到數組。這部分工作。

Delegate和DataSource插座連接到View Controller。

我想要什麼:

數組應是表視圖的數據源,所以我只是想我讀過數組中的新數據後刷新。顯示在viewDidLoad函數中添加到數組中的測試數據。所以我想我以某種方式將數組連接到表視圖。

我給你的完整代碼:

ViewController.h:

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 
{ 
    IBOutlet UILabel *output; 
    IBOutlet UITableView *tableView; 
    NSMutableData *webData; 
    NSString *finaldata; 
    NSString *convertToStringData; 
    NSMutableString *nodeContent; 
} 
@property (nonatomic, retain) UILabel *output; 
@property (nonatomic, weak) IBOutlet UITableView *tableView; 
-(IBAction)invokeService:(UIButton *) sender; 

@end 

ViewController.m:

#import "ViewController.h" 

@interface ViewController() 
{ 
    NSMutableArray *foundUrlaub; 
} 

@end 

@implementation ViewController 

@synthesize output; 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // SOME TEST DATA... THIS SHOWS UP IN MY TABLE VIEW 
    foundUrlaub = [[NSMutableArray alloc]init]; 
    [foundUrlaub addObject:@"first cell"]; 
    [foundUrlaub addObject:@"second cell"]; 
    [foundUrlaub addObject:@"third cell"]; 
} 

-(IBAction)invokeService:(UIButton *) sender 
{ 
    // connection to sharepoint 
} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSLog(@"didReceiveResponse"); 
    [webData setLength:0]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    NSLog(@"didReceiveData"); 
    [webData appendData:data]; 
} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"ERROR with the Connection"); 
    NSLog(error.description); 
} 

-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
{ 
    NSLog(@"canAuthenticateAgainstProtectionSpace"); 
    return YES; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
    NSLog(@"didReceiveAuthenticationChallenge"); 
    NSURLCredential *credential = [NSURLCredential credentialWithUser:@"XXXXXX" password:@"XXXXXX" persistence:NSURLCredentialPersistenceForSession]; 
    [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"DONE. Received Bytes: %d", [webData length]); 
    convertToStringData = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding]; 

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=ows_Title=')(.*)(?=' ows_MetaInfo)" options:0 error:NULL]; 

    NSArray *matches = [regex matchesInString:convertToStringData options:0 range:NSMakeRange(0, [convertToStringData length])]; 

    // HERE I LOAD SOME DATA IN THE ARRAY 
    [foundUrlaub removeAllObjects]; 
    for (NSTextCheckingResult *match in matches) 
    { 
     NSRange matchRange = [match rangeAtIndex:1]; 
     NSString *matchString = [convertToStringData substringWithRange:matchRange]; 
     NSLog(@"Match: %@", matchString); 
     [foundUrlaub addObject:matchString]; // <- ADDS 3 STRINGS TO ARRAY 
    } 

    // THIS DOES NOT WORK! 
    [tableView reloadData]; 

} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

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

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 

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

@end 
+0

沒有一個Xcode題。 – 2013-07-22 12:26:40

+0

您使用NSURLConnection從服務器異步獲取數據,因此它使用單獨的線程來發送和接收數據。 –

+0

嘗試按照我所說的去做,看看它的工作與否? –

回答

35

嘗試使用​​您在您的m沒有@synthesize您的tableView所以你必須使用autosynthesized標識符

+0

哈!非常感謝,現在它工作。:) – Feroc

+6

@Feroc我更喜歡使用[self.tableView reloadData]而不是下劃線。它幫助我記住tableView是類的一個屬性。事實上,我建議不要再合成了(因爲編譯器會爲你做這個)並且總是使用自己。而不是下劃線 – MobileMon

+0

它成功地重新加載,但不顯示新的數據,直到我點擊單元格,任何想法爲什麼? –

3

您的代碼看起來不錯,

確保

  1. IBOutlet中的的tableView正確連接
  2. 數據源,並從筆尖代表連接到文件的所有者

A must watch and a must read for you

+0

我沒有任何筆尖,我正在使用故事板,這讓我更加困惑。我也不知道如何將IBOutlet連接到tableView。將「恢復ID」設置爲「tableView」還不夠嗎?或者我必須將東西拖放到某個地方? – Feroc

+0

故事板和筆尖在插座的情況下都做同樣的事情。只需右鍵單擊桌面視圖並將引用插座拖到文件所有者/視圖控制器,在那裏它將顯示桌面視圖並選擇它 –

+0

插座讓代碼知道參考點在情節提要/筆尖 –

1

你必須出口處的InterfaceBuilder中連接到你的UITableview tableView。

+0

我該怎麼做? – Feroc

+0

打開您的.xib文件,然後在左側窗格上選擇「文件所有者」。在右邊的窗格中點擊最右邊的圖標(右箭頭)並從氣泡右側選擇你的「tableview」元素,然後將該行拖到你的tableview上。 –

+0

謝謝。是否 - http://i.imgur.com/eO71xmW.jpg - 仍然無法使用。 :( – Feroc

-4

您正試圖從單獨的線程重新加載表。所以UIView只能從主線程進行更改,以便做這樣的事情:

[tableView performSelectorOnMainThread:@selector(reloadData) 
          withObject:nil 
         waitUntilDone:YES]; 
+3

什麼單獨的線程?我沒有看到任何 –

+0

嘗試,因爲我已經寫了,看看它的工作與否? –

+3

這不是問題所在。 –