无法在IOS7中重新加载表视图
我对IOS开发还是一个新手,一直在与之斗争。我想显示用户在我的服务器上拥有的电话列表,但tableview不显示项目。我从服务器上很好地获得了数据,我认为UItableView的设置是正确的。这是我的密码: stkPhoneHolderServiceController.h无法在IOS7中重新加载表视图,ios,uitableview,Ios,Uitableview,我对IOS开发还是一个新手,一直在与之斗争。我想显示用户在我的服务器上拥有的电话列表,但tableview不显示项目。我从服务器上很好地获得了数据,我认为UItableView的设置是正确的。这是我的密码: stkPhoneHolderServiceController.h #import <UIKit/UIKit.h> #import "STKSimpleHttpClientDelegate.h" @interface STKPhoneHolderViewController :
#import <UIKit/UIKit.h>
#import "STKSimpleHttpClientDelegate.h"
@interface STKPhoneHolderViewController : UITableViewController <UITableViewDataSource, STKSimpleHttpClientDelegate>
@property (strong, nonatomic) IBOutlet UITableView *phoneTable;
@property (strong, nonatomic) NSMutableArray *phoneArray;
@end
STKSimpleHttpClient.m
#import "STKSimpleHttpClient.h"
#import "STKSimpleHttpClientDelegate.h"
@implementation STKSimpleHttpClient
NSMutableData *responseData;
STKHttpResult *httpResult;
void (^completeFunction)(STKHttpResult *);
- (void) send:(NSString*)url
data:(NSString*)data
{
httpResult = [[STKHttpResult alloc]init];
dispatch_async(dispatch_get_main_queue(), ^{
if ( data == nil) return;
//Get request object and set properties
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: url]];
//set header for JSON request and response
[urlRequest setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
//set http method to POST
[urlRequest setHTTPMethod:@"POST"];
//set time out
[urlRequest setTimeoutInterval:20];
NSData *body = [data dataUsingEncoding:NSUTF8StringEncoding];
//set request body
urlRequest.HTTPBody = body;
//connect to server
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
if (conn==nil){
//Do something
}
});
}
#pragma mark - NSURLConnection Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
// Return nil to indicate not necessary to store a cached response for this connection
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable noow
NSError *error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
BOOL ok = [[json objectForKey:@"ok"] boolValue];
NSString *message = [json objectForKey:@"message"];
if (ok == NO) {
[httpResult setError:message];
} else {
[httpResult setSuccess:[json objectForKey:@"result"]];
}
if (self.delegate !=nil) {
[self.delegate complete:httpResult];
}
responseData = nil;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
// Check the error var
if (self.delegate !=nil) {
[self.delegate complete:[httpResult setError:@"Connection failed."]];
}
}
STKPhoneHolder.m
#import <Foundation/Foundation.h>
@interface STKPhoneHolder : NSObject
@property NSString *deviceId;
@property NSString *userId;
@property NSString *userName;
@property NSString *msn;
- (id) initWithDeviceId:(NSString*)aDeviceId
userId:(NSString*)aUserId
userName:(NSString*)aUserName
msn:(NSString*)aMsn;
@end
我可以在“complete”方法中看到带有两个电话的日志打印phoneArray,但tableview仅显示“No record”。即使我调用了reloadData方法,Tableview也不会再次渲染。我确保在调试模式下调用[self.phoneTable reloadData]。
我还需要做些什么?不需要指定节数,但您可能需要使用以下代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
我看到您正在使用一个表视图控制器,它已经有一个tableView引用
self.tableView
正如@rdelmar所说,您可以使用该引用而不是您的电话表:
[[self tableView] setDataSource:self];
[[self tableView] setDelegate:self];
[[self tableView] reloadData];
尝试在主线程中调用重载数据
#pragma STKSimpleHttpClientDelegate
-(void) complete:(STKHttpResult*) result
{
if (result.ok != YES){
[STKUtility alert:result.message];
return;
}
self.phoneArray = (NSMutableArray*)result.result;
for (STKPhoneHolder *holder in self.phoneArray) {
NSLog(@"%@", [holder description]);
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.phoneTable reloadData];
}
}
也可以使用performSelectorOnMainThread
[self.phoneTable performSelectorOnMainThread:@selector(reloadData)
withObject:nil
waitUntilDone:NO];
我猜STKSimpleHttpClient类正在调用不同线程上的完整委托函数,所有用户界面交互都应该从主线程调用
尝试以下代码,从完整的委托函数中查看您所在的线程
NSLog(@" isMainThread(%d)", [NSThread isMainThread] );
看看这个。代码是否在从web服务获取信息之前加载tableview。如果是,则编写语句[tableview Reload];接下来是web服务信息获取过程。这将有助于@MaKo设置数据源,为什么要设置委托?您不需要创建表视图的出口。table view控制器已经具有tableView属性(您是否在IB中连接了phoneTable插座?)。尝试在numberOfRowsInSection中用self.TableView替换任何self.phoneTable引用,放入NSLog并打印self.phoneArray计数。它是否显示非零?添加一个
NSLog(@“我的表:%@”,self.phoneTable)在viewdiload
中进行code>并验证它是否为nil
。为什么在调用loadPhoneList
之前,您要alloc init
电话数组,然后再次将其作为方法的第一行?这不是必需的。默认值为1。我是在情节提要中完成的,是否也必须在源代码中再次执行?我不能显示No record而不是phoneArray,这意味着你提到的方法与我的问题无关。最后,我添加了您的代码,但它不起作用。不管怎样,谢谢。你设置了故事板的节数了吗?您是否也设置为静态表而不是动态表?应该是后者。对不起,我误解了。我没有在故事板中设置原型单元。无论如何,我添加了你的代码,但它不起作用。UITableView是动态原型,多个选择和其他属性是默认的。这是OP应该做的,但不会导致他的问题。他使用的方法是数据源方法,而不是委托方法@Guillerme,对不起,您的代码不工作。+1似乎是一个很好的猜测-应该值得再次检查。我添加了STKSimpleHttpClient。很抱歉,您的代码无效。我从STKSimpleHttpClient.m的send方法中删除了dispatch\u async,但仍然相同。
[self.phoneTable performSelectorOnMainThread:@selector(reloadData)
withObject:nil
waitUntilDone:NO];
NSLog(@" isMainThread(%d)", [NSThread isMainThread] );