reloadData调用numberOfSections,numberOfRows,而不是cellForRowAtIndexPath [英] reloadData calls numberOfSections, numberOfRows, not cellForRowAtIndexPath

查看:241
本文介绍了reloadData调用numberOfSections,numberOfRows,而不是cellForRowAtIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先抱歉,如果格式不正确,第一次这样做。我一直在使用stackoverflow来寻求帮助很长一段时间,它非常有帮助(谢谢大家),但这是我第一次发布自己的问题。这个问题已被多次询问,但是当我调用[myTable reloadTable]时,调用方法numberOfSectionsInTableView和numberOfRowsInSection但不调用cellForRowAtIndexPath。

first of all sorry if this isn't formatted correctly, first time doing this. I've been using stackoverflow to find help for a long time now and it's been very helpful (thank you all), but this is the first time I've posted a question of my own. This question has been asked many times, but when I call [myTable reloadTable] the methods numberOfSectionsInTableView and numberOfRowsInSection are called but not cellForRowAtIndexPath.

我在搜索时看到的每个答案一直是变化:
1)tableView是nil
2)numberOfRowsInSection是0
3)tableView的委托/数据源未设置
4)在错误的uiTableView上调用reloadTable 。

Every answer I've seen when searching has been a variation of: 1) The tableView is nil 2) numberOfRowsInSection is 0 3) tableView's delegate/data source not set 4) calling reloadTable on the wrong uiTableView.

对我来说,情况都不是这样,所以我想知道还有什么不对。

None of these are the case for me so I'm wondering what else could be wrong.

我正在尝试做什么:
我有一个自定义的uitableviewcell,上面有一个按钮,当按下按钮时我想改变单元格的属性(举个例子,我只想说我要改变它单元格标题为按下按钮)。然后我想暂停片刻然后删除单元格。

What I'm trying to do: I have a custom uitableviewcell with a button on it, when the button is pressed I want to change the attributes of the cell (for an example, let's just say I want to change the cell title to "Button Pressed"). I then want to pause for a moment and then delete the cell.

不确定这是否重要,但我的tableView在UIViewController中,而viewController是委托和dataSource。

Not sure if this is important, but my tableView is inside a UIViewController, and the viewController is the delegate and dataSource.

我有一个方法(下面),当按下按钮时触发,并且根据需要更改单元格属性,但是当我尝试刷新表格时显示它不起作用的更改。第一次调用[remTable reloadData]时,会调用numberofsectionsintableview和numberofrowsinsection,但不会调用cellforrowatindexpath。但是,第二次[remTable reloadData]被调用时,所有三个方法都被调用,一切正常。

I have a method (below) that fires when the button is pressed and the cell attributes are changed as necessary, however when I try to refresh the table to show the changes it doesn't work. The first time [remTable reloadData] is called, numberofsectionsintableview and numberofrowsinsection are called but not cellforrowatindexpath. However, the second time [remTable reloadData] is called all three methods are called and everything works correctly.

doneCell.remName.text=@"BUTTON PRESSED";
[remTable reloadData];
NSLog(@"sleep");
sleep(1);
[remList removeObject:doneReminder];
[remTable reloadData];

为了测试这个我把nslog语句放在numberofsections,numberofrows和cellforrow的开头,输出是方法的名称后跟数字(对于numberofsections / numberofrows)。

To test this I put nslog statements at the beginning of numberofsections, numberofrows and cellforrow, the output is the name of the method followed by the number (for numberofsections/numberofrows).

输出:

numberofsections 1
numberofrows 3
sleep
numberofsections 1
numberofrows 2
cellforrow

关于为什么第一次没有调用cellforrow的想法?在此先感谢,如果我还有其他任何内容可以澄清,请告诉我。

Any ideas as to why cellforrow's not being called the first time? Thanks in advance, please let me know if there's anything else I can add to clarify.

推荐答案

表格视图实际上并不存在请求它的单元格,直到它需要显示它们。当你调用 reloadData 时,它会立即请求段和行的数量,以便它知道它需要多大。在要求显示自己之前,它不会要求细胞本身。在每个运行循环开始时,视图会根据需要自动显示。

A table view doesn't actually request its cells until it needs to display them. When you call reloadData, it will immediately request the number of sections and rows so that it knows how big it needs to be. It doesn't ask for the cells themselves until it is asked to display itself. Views are automatically displayed as necessary at the beginning of each run loop.

在代码返回之前,执行不会返回到运行循环。当你打电话给 sleep 时,你不会执行任何代码,但你也不会返回。这意味着运行循环没有机会显示表,因此它永远不会要求任何单元格。你需要做的是从你的方法返回并要求在1秒内调用另一个方法来删除 doneReminder 。一种简单的方法是使用 performSelector:withObject:afterDelay:方法。此方法添加一个计时器,该计时器将在给定的延迟后调用您请求的方法,这意味着您可以返回到运行循环并让表格显示。

Execution doesn't return to the run loop until your code returns. When you call sleep, you don't execute any code, but you also don't return. This means the run loop doesn't get a chance to display the table, so it never asks for any cells. What you need to do is return from your method and ask that another method gets called in 1 second to remove the doneReminder. An easy way to do this is to use the performSelector:withObject:afterDelay: method. This method adds a timer which will call the method you requested after the given delay, which means you can return to the run loop and let the table display.

doneCell.remName.text=@"BUTTON PRESSED";
[remTable reloadData];
[self performSelector:@selector(removeDoneReminder) withObject:nil afterDelay:1.0];





- (void)removeDoneReminder {
    [remList removeObject:doneReminder];
    [remTable reloadData];
}

如果 remlist remTable 不是实例变量,您可以使用 withObject:参数将它们发送到 removeDoneReminder 方法。

If remlist and remTable are not instance variables, you can use the withObject: parameter to send them to the removeDoneReminder method.

这篇关于reloadData调用numberOfSections,numberOfRows,而不是cellForRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆