刷新表格视图?

我觉得我有一个非常容易的任务,但不知何故,它不想工作。 我是Objective-C的初学者,所以我想这只是一个小错误。 我仍然不知道自己在做什么,目前更像是复制和粘贴编程。 就像我真的不知道我是否需要在界面中使用IBOutlet,或者作为一个属性或两者兼而有之。

我拥有的:

带有button,标签和表格视图的ViewController。 该button连接到共享点服务器并读取一个列表并将该值添加到数组。 这部分工作。

Delegate和DataSourcesockets连接到View Controller。

我想要的是:

该数组应该是表视图的数据源,所以我只是想在它读取数组中的新数据后刷新。 显示在viewDidLoad函数中添加到数组中的testing数据。 所以我想我以某种方式将数组连接到表视图。

我会给你完整的代码:

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 

尝试使用[_tableView reloadData]; 你没有在你的.m中使用@synthesize你的tableView,所以你必须使用自动合成的标识符

你的代码看起来不错,

确保

  1. 用于tableView的IBOutlet已正确连接
  2. 来自nib的数据源和委托连接到文件所有者

一定要看,一定要为你读

你必须将Interfacebuilder中的Outlet连接到你的UITableview tableView。

您正试图从单独的线程重新加载表。 所以UIView只能从主线程改变,所以做这样的事情:

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