我使用storyboard创建了一个 UITableViewController ,并且希望每次检测到设备时都重新加载 tableview(对于 bonjour 协议)。但是,reload data 方法只调用 numberofRows,而不调用 CellForRowatIndexPath。所以我没有任何改变。我检查的 mutablearray 总是正确的。添加了新对象。但是 tableview 不会随着数组而改变。所以我想知道我是否在这里设置错了。
从我添加的 NSLog 中,我发现,我可以在开头使用初始化数组加载 tableview
self.serviceArray = [[NSMutableArray alloc]initWithObjects:@"test", nil];
但在方法上
- (void)addService:(NSNetService *)....
每次重新加载时,新对象都可以添加到 mutableArray 中,但 tableview 不会随数组而改变。(它调用了numberOfRows,我检查返回的数组大小不是0,但是它没有调用cellOfRowAtIndex)
这是我的代码
.h 文件
#import <UIKit/UIKit.h>
#import "Server.h"
@interface BrowserViewController : UITableViewController <ServerDelegate>
{
Server *_server;
NSMutableArray *_serviceArray;
}
@property (retain,nonatomic) NSMutableArray *serviceArray;
@property (nonatomic,retain) Server *server;
- (void)addService:(NSNetService *)service moreComing:(BOOL)more;
@end
.m 文件
#import "BrowserViewController.h"
@implementation BrowserViewController
@synthesize serviceArray = _serviceArray;
@synthesize server = _server;
- (void) dealloc
{
[self.serviceArray release];
[self.tableView release];
[super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
self.title = @"Service Browser";
_serviceArray = nil;
self.serviceArray = nil;
[self.tableView setDelegate: self];
[self.tableView setDataSource:self];
//actually I set this in the storyboard already
[self.tableView reloadData];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
_serviceArray = nil;
NSLog(@"viewwilldisappear");
}
- (NSMutableArray *)serviceArray {
if(nil == _serviceArray) {
self.serviceArray = [[NSMutableArray alloc]initWithObjects:@"test", nil];
}
else
{
NSLog(@"update array");
}
return _serviceArray;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)addService:(NSNetService *)service moreComing:(BOOL)more{
[self.serviceArray addObject:service];
if (!more) {
[self.tableView reloadData];
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return @"Connection Choices";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.serviceArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *Cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (Cell == nil) {
Cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSString *text = [self.serviceArray objectAtIndex:indexPath.row];
Cell.textLabel.text = text;
return Cell;
}
@end
我检查了一些解决方案,它要么是委托设置问题,要么是数组为空。或单元格初始化时没有空格。有什么方法可以检查某个方法的委托是什么?
我还尝试在.h文件中创建一个tableview属性,链接IBOutlet,然后在.m中合成。但还是不行。有人可以帮我吗?
由于很多教程都是基于基于窗口的应用程序,有人可以为我提供一些使用故事板和使用 reloadData 的教程吗?我可以检查一下奥特莱斯是不是错了。
提前致谢。
UPDATE1 AddService 方法在 appDelegate 中被调用
appDelegate.h
#import <UIKit/UIKit.h>
#import "Server.h"
@class BrowserViewController;
@interface iphoneNetworkAppDelegate : NSObject <UIApplicationDelegate,UITableViewDataSource, UITableViewDelegate, ServerDelegate>
{
Server *_server;
UIWindow *window;
IBOutlet BrowserViewController *BrowserVC;
}
@property (strong, nonatomic) UIWindow *window;
@end
appDelegate.m
#import "iphoneNetworkAppDelegate.h"
#import "BrowserViewController.h"
@implementation iphoneNetworkAppDelegate
@synthesize window;
- (void)dealloc
{
[window release];
[_server release];
_server = nil;
[BrowserVC release];
BrowserVC = nil;
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSString *type = @"TestingProtocol";
_server = [[Server alloc] initWithProtocol:type];
_server.delegate = self;
NSError *error = nil;
if(![_server start:&error]) {
NSLog(@"error = %@", error);
}
BrowserVC = [BrowserViewController alloc];
if (BrowserVC == nil) {
NSLog(@"need to init");
}
BrowserVC.server = _server;
return YES;
}
#pragma mark Server Delegate Methods
- (void)serverRemoteConnectionComplete:(Server *)server {
NSLog(@"Server Started");
BrowserVC.server = server;
}
- (void)serverStopped:(Server *)server {
NSLog(@"Server stopped");
}
- (void)server:(Server *)server didNotStart:(NSDictionary *)errorDict {
NSLog(@"Server did not start %@", errorDict);
}
- (void)server:(Server *)server didAcceptData:(NSData *)data {
NSLog(@"Server did accept data %@", data);
}
- (void)server:(Server *)server lostConnection:(NSDictionary *)errorDict {
NSLog(@"Server lost connection %@", errorDict);
}
- (void)serviceAdded:(NSNetService *)service moreComing:(BOOL)more {
NSLog(@"service added in delegate");
[BrowserVC addService:service moreComing:more];
}
#pragma mark -
- (void)applicationWillTerminate:(UIApplication *)application {
[_server stop];
[_server stopBrowser];
}
如果appDelegate设置是这样,reloadData就不能调用了?