- UITableViewCell的重用機(jī)制體現(xiàn)在-(UITableViewCell)dequeueReusableCellWithIdentifier:(NSString)identifier這個(gè)方法中纪岁,他的基本意思就是在創(chuàng)建cell的時(shí)候?yàn)槊恳粋€(gè)cell都綁定一個(gè)identifier的標(biāo)識笔横。當(dāng)cell從我們的視覺范圍中消失的時(shí)候箩溃,這個(gè)綁定了cell的標(biāo)識就會被放到緩存池中。當(dāng)tableView需要新的cell的時(shí)候锻离,直接先去緩存池中尋找有沒有攜帶identifier的cell,若有的話直接復(fù)用纱兑;沒有的話,才去創(chuàng)建新的cell精肃,并綁定標(biāo)識identifier秤涩。所以從理論上講,倘若一屏最多顯示的cell個(gè)數(shù)為n個(gè)司抱,那么需要攜帶identifier表示的cell最少只需n+1個(gè)筐眷。
- 當(dāng)然在某些情況下,我們不需要復(fù)用cell习柠,因?yàn)閏ell的復(fù)用在當(dāng)cell的個(gè)數(shù)比較少的情況下匀谣,對于性能的提升幾乎沒有作用,反而有可能會造成數(shù)據(jù)的錯(cuò)亂资溃,這時(shí)候我們不想再使用cell的重用機(jī)制武翎,這時(shí)候該怎么辦呢?可以利用以下幾種方式來解決:
方法1 將獲得cell的方法從- (UITableViewCell*)dequeueReusableCellWithIdentifier:(NSString*)identifier 換為-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
重用機(jī)制調(diào)用的就是dequeueReusableCellWithIdentifier這個(gè)方法肉拓,方法的意思就是“出列可重用的cell”,因而只要將它換為cellForRowAtIndexPath(只從要更新的cell的那一行取出cell)梳庆,就可以不使用重用機(jī)制暖途,因而問題就可以得到解決,雖然可能會浪費(fèi)一些空間膏执。
示例代碼:
[plain]
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改為以下的方法
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根據(jù)indexPath準(zhǔn)確地取出一行驻售,而不是從cell重用隊(duì)列中取出
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//...其他代碼
}(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改為以下的方法
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根據(jù)indexPath準(zhǔn)確地取出一行,而不是從cell重用隊(duì)列中取出
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//...其他代碼
}
方法2 通過為每個(gè)cell指定不同的重用標(biāo)識符(reuseIdentifier)來解決更米。
重用機(jī)制是根據(jù)相同的標(biāo)識符來重用cell的欺栗,標(biāo)識符不同的cell不能彼此重用。于是我們將每個(gè)cell的標(biāo)識符都設(shè)置為不同征峦,就可以避免不同cell重用的問題了迟几。
示例代碼:
[plain]
-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath來唯一確定cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//...其他代碼
} -
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath來唯一確定cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//...其他代碼
}
方法3 刪除重用cell的所有子視圖
這個(gè)方法是通過刪除重用的cell的所有子視圖,從而得到一個(gè)沒有特殊格式的cell栏笆,供其他cell重用类腮。
示例代碼:
[plain]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
else
{
//刪除cell的所有子視圖
while ([cell.contentView.subviews lastObject] != nil)
{
[(UIView)[cell.contentView.subviews lastObject] removeFromSuperview];
}
}
//...其他代碼
}
- js中的邏輯與(&&)和邏輯或(||):
在js中寫出如下的答案 :
var a = 2;
var b = 3;
var andflag = a && b ;
var orflag = a || b;
乍一看,這是一道邏輯或和邏輯與的運(yùn)算題,然而在webostorm中運(yùn)行一下才發(fā)現(xiàn)結(jié)果是,andflag = 3,orflag = 2;
具體的執(zhí)行原理如下:
在運(yùn)算過程中,首先js 會將 && 和|| 兩邊的值轉(zhuǎn)成Boolean 類型蛉加,然后再算值 蚜枢,&&運(yùn)算如果返回true,
則取后面的值针饥,如果|| 返回true,則取前面的值 厂抽, 而其中數(shù)值轉(zhuǎn)換成boolean 的規(guī)則 是:
對象、非零整數(shù)丁眼、非空字符串返回true,
其它為false ;