ios 刷新表视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17787480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh table view?
提问by Feroc
I think I have a pretty easy task, but somehow it doesn't want to work. I am a total beginner in objective-c, so I guess it's just a small mistake. I still don't really know what I do, currently it's more like copy&paste programming. Like I don't really know if I need the IBOutlet in the interface or as a property or as both.
我想我有一个非常简单的任务,但不知何故它不想工作。我是objective-c的初学者,所以我想这只是一个小错误。我仍然不知道我在做什么,目前它更像是复制和粘贴编程。就像我真的不知道我是否需要界面中的 IBOutlet 或作为属性或两者兼而有之。
What I have:
我拥有的:
A ViewController with a Button, a Label and a Table View. The button connects to a sharepoints server and reads a list and adds the value to an array. This part works.
一个带有按钮、标签和表格视图的 ViewController。该按钮连接到一个 sharepoints 服务器并读取一个列表并将该值添加到一个数组中。这部分有效。
Delegate and DataSource outlet is connected to the View Controller.
委托和数据源插座连接到视图控制器。
What I want:
我想要的是:
The array should be the datasource of the Table View, so I just want it to refresh after I've read the new data in the array. The test data I add in the viewDidLoad function to the array, shows up. So I guess I somehow connected the array to the table view.
数组应该是表视图的数据源,所以我只想在读取数组中的新数据后刷新它。我在 viewDidLoad 函数中添加到数组的测试数据出现了。所以我想我以某种方式将数组连接到表视图。
I'll give you the full code:
我会给你完整的代码:
ViewController.h:
视图控制器.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:
视图控制器.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
回答by Chris Tetreault
try using [_tableView reloadData];
You didn't @synthesize
your tableView in your .m so you have to use the autosynthesized identifier
尝试使用[_tableView reloadData];
您@synthesize
的 .m 中没有您的 tableView,因此您必须使用自动合成标识符
回答by Lithu T.V
Your code looks fine ,
你的代码看起来不错,
make sure
确保
- IBOutlet for tableView is connected properly
- Datasource and delegates from nib is connected to the files owner
- tableView 的 IBOutlet 连接正确
- 来自 nib 的数据源和委托连接到文件所有者
回答by Julien Klindt
You have to connect the Outlet in the Interfacebuilder to your UITableview tableView.
您必须将 Interfacebuilder 中的 Outlet 连接到您的 UITableview tableView。
回答by Armaan Stranger
You are trying to reload table from separate thread. so UIView
can be changed only from main thread so do something like this:
您正在尝试从单独的线程重新加载表。所以UIView
只能从主线程改变所以做这样的事情:
[tableView performSelectorOnMainThread:@selector(reloadData)
withObject:nil
waitUntilDone:YES];