大綱
一莽囤、自我測(cè)試
兩個(gè)方法:1.選定 2.取消選定
二偷崩、重用TableViewCell
2.1 重用
重用機(jī)制:
1.若界面能顯示n個(gè)Cell贴妻,系統(tǒng)會(huì)創(chuàng)建n+1個(gè)缴阎。
2.系統(tǒng)會(huì)將劃出屏幕的Cell放入重用隊(duì)列。
3.當(dāng)新Cell將劃入屏幕庆杜,系統(tǒng)會(huì)先到重用隊(duì)列中尋找可重用的Cell射众,若無(wú),就重新創(chuàng)建晃财。
注意:為何要聲明重用標(biāo)識(shí)符叨橱?若單元格有多種樣式,就要通過(guò)標(biāo)識(shí)符進(jìn)行標(biāo)記断盛。
2.2 除舊創(chuàng)新
找到舊控件→移除→創(chuàng)建新控件
2.3 使用舊控件
找到舊控件→直接使用
2.4 自定義Cell
設(shè)置屬性罗洗,重寫(xiě)初始化方法
1.創(chuàng)建一個(gè)類(lèi),繼承于UITableViewCell
2.在cell.contentView上添加自己想要的控件
3.聲明屬性钢猛,將創(chuàng)建的控件賦值給屬性
2.5 拖拽Cell
正文
一伙菜、作業(yè)
兩個(gè)方法:1.選定 2.取消選定
項(xiàng)目:TableView_IphoneSetting_Teacher0405
//單元格被選定
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewController1 *vc1 = [[ViewController1 alloc]init];
NSLog(@"選中:(%d,%d)",indexPath.section,indexPath.row);
[self.navigationController pushViewController:vc1 animated:YES];
[vc1 release];
}
//單元格被取消選定
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"取消選中:(%d,%d)",indexPath.section,indexPath.row);
}
二、TableViewCell
內(nèi)部構(gòu)造:
2.1 重用
重用機(jī)制:
1.若界面能顯示n個(gè)Cell命迈,系統(tǒng)會(huì)創(chuàng)建n+1個(gè)贩绕。
2.系統(tǒng)會(huì)將劃出屏幕的Cell放入重用隊(duì)列。
3.當(dāng)新Cell將劃入屏幕壶愤,系統(tǒng)會(huì)先到重用隊(duì)列中尋找可重用的Cell淑倾,若無(wú),就重新創(chuàng)建公你。
注意:為何要聲明重用標(biāo)識(shí)符踊淳?若單元格有多種樣式,就要通過(guò)標(biāo)識(shí)符進(jìn)行標(biāo)記陕靠。
項(xiàng)目:TableViewReuseID0405
源碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.重用標(biāo)識(shí)符
static NSString *cellIdentifier = @"Cell";
//2.(從重用隊(duì)列 隨機(jī))找可重用的單元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//若無(wú)重用cell迂尝,就創(chuàng)建一個(gè)
if(cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
cell.textLabel.text = [_array objectAtIndex:indexPath.row];
return cell;
}
2.2 除舊創(chuàng)新
找到舊控件→移除→創(chuàng)建新控件
項(xiàng)目:TableView_CostumCell0405
源碼:
//移除舊圖片
UIImageView *oldImgView = (UIImageView *)[cell.contentView viewWithTag:100];
[oldImgView removeFromSuperview];
//創(chuàng)建新圖片
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(240, 10, 60, 60)];
imgView.image = [UIImage imageNamed:[_imgNameArr objectAtIndex:indexPath.row]];
imgView.alpha = 0.5;
imgView.tag = 100;
[cell.contentView addSubview:imgView];
[imgView release];
2.3 使用舊控件
找到舊控件→直接使用
項(xiàng)目:TableView_CostumCell2_Reuse_0405
源碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//創(chuàng)建新圖片
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(240, 10, 60, 60)];
imgView.tag = 100;
[cell.contentView addSubview:imgView];
}
//找到舊圖片
UIImageView *imgView = (UIImageView *)[cell.contentView viewWithTag:100];
imgView.image = [UIImage imageNamed:[_imgNameArr objectAtIndex:indexPath.row]];
return cell;
}
2.4 自定義Cell
設(shè)置屬性,重寫(xiě)初始化方法
1.創(chuàng)建一個(gè)類(lèi)剪芥,繼承于UITableViewCell
2.在cell.contentView上添加自己想要的控件
3.聲明屬性垄开,將創(chuàng)建的控件賦值給屬性
項(xiàng)目:TableView_CostumCell_CostumClass0405
源碼:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
//1.創(chuàng)建一個(gè)類(lèi),繼承于UITableViewCell
//2.在cell.contentView上添加自己想要的控件
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(240, 10, 60, 60)];
[self.contentView addSubview:imgView];
//3.聲明屬性税肪,將創(chuàng)建的控件賦值給屬性
self.customImageView = imgView;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 100, 40)];
[self.contentView addSubview:label];
self.customLabel = label;
}
return self;
}
2.5 拖拽Cell
項(xiàng)目:TableView_CustomCell_Drag0405
1.拖拽多個(gè)UITableViewCell如何獲取
自我測(cè)試:
1.美團(tuán)
2.一個(gè)tableView中有兩種TableViewCell溉躲。