UITableView刷新问题

发布于 2024-10-21 13:48:17 字数 2625 浏览 5 评论 0 原文

每次导航包含此表的视图时,我都尝试刷新 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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无法回应 2024-10-28 13:48:17

在此代码示例中存在许多问题。我将在这里指出其中的一些,但我强烈建议您阅读相关的 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

代码中的一些问题:

  1. 由于 MyTableController 类是 UITableViewController 的子类,因此您不需要 myTableView 的属性和属性>。 tableView 属性作为 UITableViewController 实现的一部分进行定义和初始化,其 dataSource 和委托设置为 UITableViewController 实例。这就是为什么 [self.tableView reloadData] 调用您的委托和 dataSource 协议方法。

  2. 您还在使用界面生成器,因此如果您确实想创建自己的子视图,您应该在 IB 中执行此操作并在那里设置出口,或者在代码中执行此操作,这意味着在 viewDidLoad 中创建子视图 然后使用 [view addSubview:mySubView] 将它们添加到您的视图中。

  3. 为表设置数据的更好方法是为 data 属性创建一个属性,并从已初始化 data 的视图控制器调用 setData >MyTableController 实例。您可以使用 setData: 方法来执行此操作。您可以在setData中调用[self.tableView reloadData]。加载视图时,您不需要显式地重新加载表,因为这是自动完成的。 是,如果您继续使用 EditTable,我会将其重命名为更具描述性,并使用驼峰式大小写(例如 setDataForTable`)以与 iOS 约定保持一致。

  4. 您没有为 tableView:cellForRowAtIndexPath: 中引用的 listenom 属性显示任何 init/alloc。您是否打算使用 data 来代替?

  5. 您的MyTableController.m 文件是完整版本吗?如果是这样,则您缺少 viewDidUnloaddealloc 方法。两者都是必需的。 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:

  1. Since the class MyTableController is a subclass of UITableViewController you don't need the attribute and property for myTableView. The tableView 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.

  2. 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].

  3. A better way to set the data for your table would be to create a property for your data attribute and call setData from the view controller that has initialized the MyTableController instance. You would use the setData: method to do this. You can call [self.tableView reloadData] in setData. 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 with EditTable I would rename it to be more descriptive and to use camel case (e.g. setDataForTable`) to be consistent with iOS conventions.

  4. You don't show any init/alloc for the listenom attribute referenced in tableView:cellForRowAtIndexPath:. Did you mean to use data instead?

  5. Is your MyTableController.m file the complete version? If so, you are missing viewDidUnload and dealloc methods. Both of which are required. viewDidUnload should release any objects allocated in viewDidLoad and dealloc should release anything retained by the controller (including objects released in viewDidUnload.

述情 2024-10-28 13:48:17

当您使用 tableViewController 时,您应该能够使用 self.tableView 来重新加载数据,如下所示

[self.tableView reloadData];

As you are using tableViewController you should be able to use self.tableView instead to reload the data like this

[self.tableView reloadData];
萌面超妹 2024-10-28 13:48:17

你需要先综合然后你可以使用 self.myTable

在上面做

@synthesize myTable

然后

[self.myTable reloadData];

you need to synthesize first then you can use self.myTable

do on the top

@synthesize myTable

and then

[self.myTable reloadData];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文