每次导航包含此表的视图时,我都尝试刷新 UITableView
。
我有一个 ViewController 和一个自定义 UITableViewController,它可以在应用程序启动时使用控制器内包含的 NSMutableArray 正确设置表。
当我导航到包含该表的页面时,ViewController
调用一个函数,该函数通过 HTTP 请求从服务器获取数据,并在 NSMutableArray
中解析它。
现在这是我的问题。我设法将此数组发送到我的 UITableViewController
,但是当我想刷新我的 tableView 时,什么也没有发生。
我尝试使用 [myTable reloadData]
,但它不会调用 numberOfRowsInSection 或 cellForRowAtIndexPath
函数。我看到有同样问题的人使用 [self.myTable ReloadData]
解决了它,但我收到错误:
访问未知的 getter/setter 方法
我对 Objective-C 还很陌生,这个错误对我来说仍然有点神秘,因为我有点随机地得到它。
不管怎样,我很可能搞乱了 UITableViewController 的声明(我应该在哪里声明它?)以及 Interface Builder 链接,所以这可以作为查找的线索解决方案。
有人有想法吗?
非常感谢!
编辑:这是我的表视图控制器类:
#import "MyCell.h"
@class Mycell;
@interface MyTableController : UITableViewController {
IBOutlet MyCell * myCell;
IBOutlet UITableView * myTable;
NSMutableArray *data;
}
@property (nonatomic, retain) IBOutlet UITableView * myTable;
- (void) EditTable : (NSMutableArray*) param;
@end
现在是.m:
@implementation MyTableController
@synthesize myTable;
- (void) viewDidLoad {
[super viewDidLoad];
myTable = [[UITableView alloc] init];
data = [[NSMutableArray alloc] init];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"MyCell";
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; >
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (MyCell *) currentObject;
}
}
}
NSString *datastring = [listenom objectAtIndex:indexPath.row];
[cell setCell: datastring ];
return cell;
}
- (void) EditTable : (NSMutableArray*) param{
//This function is called by the ViewController when the user goes to the page containing the view
data = param; //The param array contains the data from the HTTP request
[self.tableView reloadData];
[self.myTable reloadData]; //I tried both, but only the first one actually calls the previous functions
}
I am trying to refresh an UITableView
every time I navigate the the view that contains this Table.
I Have a ViewController
and a custom UITableViewController
that manages to set the Table Correctly when the application starts, using an NSMutableArray
contained inside the controller.
When I navigate to the page containing the table, the ViewController
calls a function that gets the data from a server with an HTTP request and parse it in an NSMutableArray
.
Now here is my problem. I manage to send this array to my UITableViewController
, but when I want to refresh my tableView, nothing happens.
I tried to use [myTable reloadData]
, but it doesn't calls the numberOfRowsInSection, or cellForRowAtIndexPath
functions. I saw that people with the same problem solved it using [self.myTable ReloadData]
, but I get an error :
accessing unknown getter/setter method
I am pretty new to objective-C, and this error is still a bit mysterious to me as I get it a bit randomly.
Anyway, there is a high probability that I made a mess with the declaration of the UITableViewController
(where am I supposed to declare it?) and with the Interface Builder links, so this can be a clue to find the solution.
Any one have an idea?
Thank you very much!
EDIT : Here is my tableview controller class:
#import "MyCell.h"
@class Mycell;
@interface MyTableController : UITableViewController {
IBOutlet MyCell * myCell;
IBOutlet UITableView * myTable;
NSMutableArray *data;
}
@property (nonatomic, retain) IBOutlet UITableView * myTable;
- (void) EditTable : (NSMutableArray*) param;
@end
And now the .m:
@implementation MyTableController
@synthesize myTable;
- (void) viewDidLoad {
[super viewDidLoad];
myTable = [[UITableView alloc] init];
data = [[NSMutableArray alloc] init];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"MyCell";
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; >
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (MyCell *) currentObject;
}
}
}
NSString *datastring = [listenom objectAtIndex:indexPath.row];
[cell setCell: datastring ];
return cell;
}
- (void) EditTable : (NSMutableArray*) param{
//This function is called by the ViewController when the user goes to the page containing the view
data = param; //The param array contains the data from the HTTP request
[self.tableView reloadData];
[self.myTable reloadData]; //I tried both, but only the first one actually calls the previous functions
}
发布评论
评论(3)
在此代码示例中存在许多问题。我将在这里指出其中的一些,但我强烈建议您阅读相关的 Apple 文档:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html
和
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/AboutTableViewsiPhone /AboutTableViewsiPhone.html
代码中的一些问题:
由于
MyTableController
类是 UITableViewController 的子类,因此您不需要myTableView
的属性和属性>。tableView
属性作为 UITableViewController 实现的一部分进行定义和初始化,其 dataSource 和委托设置为 UITableViewController 实例。这就是为什么 [self.tableView reloadData] 调用您的委托和 dataSource 协议方法。您还在使用界面生成器,因此如果您确实想创建自己的子视图,您应该在 IB 中执行此操作并在那里设置出口,或者在代码中执行此操作,这意味着在 viewDidLoad 中创建子视图 然后使用
[view addSubview:mySubView]
将它们添加到您的视图中。为表设置数据的更好方法是为
data
属性创建一个属性,并从已初始化data 的视图控制器调用
实例。您可以使用setData
>MyTableControllersetData:
方法来执行此操作。您可以在setData
中调用[self.tableView reloadData]
。加载视图时,您不需要显式地重新加载表,因为这是自动完成的。 是,如果您继续使用EditTable
,我会将其重命名为更具描述性,并使用驼峰式大小写(例如 setDataForTable`)以与 iOS 约定保持一致。您没有为
tableView:cellForRowAtIndexPath:
中引用的listenom
属性显示任何 init/alloc。您是否打算使用data
来代替?您的
MyTableController.m
文件是完整版本吗?如果是这样,则您缺少viewDidUnload
和dealloc
方法。两者都是必需的。viewDidUnload
应释放在viewDidLoad
中分配的所有对象,而dealloc
应释放控制器保留的任何对象(包括在viewDidUnload
中释放的对象) >.You have a number of problems in this code sample. I'll point out a few of them here but I highly recommend reading the relevant Apple documentation at:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html
and
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html
Some issues in your code:
Since the class
MyTableController
is a subclass of UITableViewController you don't need the attribute and property formyTableView
. ThetableView
property is defined and initialized as part of UITableViewController's implementation with its dataSource and delegate set to the UITableViewController instance. This is why [self.tableView reloadData] is calling your delegate and dataSource protocol methods.You are also using interface builder so if you did want to create your own subviews you should either do that within IB and set the outlet there or do it in your code which means creating the subview(s) in
viewDidLoad
and then adding them to your view with[view addSubview:mySubView]
.A better way to set the data for your table would be to create a property for your
data
attribute and callsetData
from the view controller that has initialized theMyTableController
instance. You would use thesetData:
method to do this. You can call[self.tableView reloadData]
insetData
. You don't need to explicitly reload the table when the view is loaded as this is done automatically. A more minor point, if you stay withEditTable
I would rename it to be more descriptive and to use camel case (e.g. setDataForTable`) to be consistent with iOS conventions.You don't show any init/alloc for the
listenom
attribute referenced intableView:cellForRowAtIndexPath:
. Did you mean to usedata
instead?Is your
MyTableController.m
file the complete version? If so, you are missingviewDidUnload
anddealloc
methods. Both of which are required.viewDidUnload
should release any objects allocated inviewDidLoad
anddealloc
should release anything retained by the controller (including objects released inviewDidUnload
.当您使用 tableViewController 时,您应该能够使用 self.tableView 来重新加载数据,如下所示
As you are using tableViewController you should be able to use
self.tableView
instead to reload the data like this你需要先综合然后你可以使用 self.myTable
在上面做
然后
you need to synthesize first then you can use self.myTable
do on the top
and then