1. tableview reloadData
假设你有一个 tableview ,你的 cell 中有 UITextField 或者 UITextView,而且你在编辑这一行的数据。此时对文本框进行监听,然后调用了 reloadData
,键盘就消失了。
/*Reloads everything from scratch. Redisplays visible rows.
Note that this will cause any existing drop placeholder rows
to be removed.*/
- (void)reloadData;
api里面是这么描述的
Call this method to reload all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on. For efficiency, the table view redisplays only those rows that are visible. It adjusts offsets if the table shrinks as a result of the reload. The table view’s delegate or data source calls this method when it wants the table view to completely reload its data. It should not be called in the methods that insert or delete rows, especially within an animation block implemented with calls to
beginUpdates
andendUpdates
.
划重点:Call this method to reload all the data that is used to construct the table, including cells, section headers and footers, index arrays, and so on..
在你进行编辑的时候,你的 cell 中的UITextView
或者UITextField
是 first responder。但是 reloadData
reload了所有的数据,包括了你的表头,表尾,单元格等所有的数据。因为你的单元格要被重载,所以你的UITextView
和UITextField
就不是 first responder。那么相应的表现出来的就是你的键盘会被收起来
2. tableview reloadRowsAtIndexPaths:withRowAnimation:
api 里面的描述
Reloading a row causes the table view to ask its data source for a new cell for that row. The table animates that new cell in as it animates the old row out. Call this method if you want to alert the user that the value of a cell is changing. If, however, notifying the user is not important—that is, you just want to change the value that a cell is displaying—you can get the cell for a particular row and set its new value.
划重点:Reloading a row causes the table view to ask its data source for a new cell for that row. The table animates that new cell in as it animates the old row out.
假设你在编辑 A行的数据,此时A中有和这个数据绑定的刷新,那么你reloadRowsAtIndexPaths:withRowAnimation 。你调用此方法会导致旧的cell消失,而新的 cell出现后并没有被注册成为 first responder 。所以键盘也会消失。 但是在 A行刷新 B行的数据,则不会出现此问题。
3.table beginUpdates & endUpdates
关于 beginUpdates 的描述
Call this method if you want subsequent insertions, deletion, and selection operations (for example,
cellForRowAtIndexPath:
andindexPathsForVisibleRows
) to be animated simultaneously. You can also use this method followed by theendUpdates
method to animate the change in the row heights without reloading the cell. This group of methods must conclude with an invocation ofendUpdates
. These method pairs can be nested. If you do not make the insertion, deletion, and selection calls inside this block, table attributes such as row count might become invalid. You should not callreloadData
within the group; if you call this method within the group, you must perform any animations yourself.
划重点: You can also use this method followed by the endUpdates
method to animate the change in the row heights without reloading the cell
假设你需要动态的修改 cell 的高度,你可以调用此方法,这样就可以动态的改变单元格的高度。如果你需要一组动画,同时进行插入和删除的话,那么也可以使用这个方法。但是不要在此调用reloadData
方法,如果你需要reloadData 在此方法中,那么你需要自己实现动画
总结
最近在 tableview中挣扎了好久,闲下来把 tableview 数据刷新的一些坑整理了一下。接下来还有动态单元格,抽空也尽量的都写出来。尽量少让后人采坑