iPhone開(kāi)發(fā)應(yīng)用中關(guān)于UITableView詳細(xì)教程是本文要介紹的內(nèi)容下隧,主要是來(lái)學(xué)習(xí)UITableView的詳細(xì)操作施无。UITableView是一個(gè)很強(qiáng)大的控件僚害,在我們iphone開(kāi)發(fā)過(guò)程中會(huì)經(jīng)常用到摸航。
下面我做以下簡(jiǎn)單介紹
?UITableView有一個(gè)基本元素的索引NSIndexPath奥帘,你可以通過(guò)索引NSIndexPath找到?UITableView下面的子元素只要這個(gè)方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//在這個(gè)方法里你可以根據(jù)NSIndexPath判斷相應(yīng)的元素铜邮,然后做處理
例如:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
{
if(indexPath.section == 5 && indexPath.row ==2)return;
}
UITableView的每一個(gè)子元素(Entity)稱為UITableViewCell寨蹋,UITableViewCell由下面這個(gè)方法初始化
-(UITableViewCell)tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath)indexPath
//這個(gè)方法是必需的松蒜,他是產(chǎn)生UITableView內(nèi)容的必須載體
例如:
-(UITableViewCell)tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath)indexPath
{//因?yàn)閁ITableView由很好的內(nèi)存控制機(jī)制,他每次只加載一屏幕的cell(7個(gè)左右)已旧,當(dāng)用戶觸摸移動(dòng)時(shí)牍鞠,動(dòng)態(tài)加載新產(chǎn)生的cell
static NSString RootViewControllerCell =@"HelpViewControllerCell";//產(chǎn)生一個(gè)靜態(tài)標(biāo)識(shí)
UITableViewCell cell =(UITableViewCell)[tableViewdequeueReusableCellWithIdentifier:RootViewControllerCell];//標(biāo)記新產(chǎn)生的cell
if(cell==nil) //如果cell不為空標(biāo)識(shí)cell還沒(méi)有被釋放,還在屏幕中顯示
{
cell =[[[UITableViewCellalloc]initWithStyle:UITableViewCellSeparatorStyleSingleLinereuseIdentifier:RootViewControllerCell]autorelease];
}
return cell评姨;
}
UITableViewCell通過(guò)NSIndexPath索引难述,正如上面所看到的,NSIndexPath索引由兩個(gè)屬性section和row
section通常就是我們所謂的段吐句,row所謂的段內(nèi)的行
我們可以通過(guò)下面這個(gè)方法返回你想要在UITableView中顯示段數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; //通常情況下我們看到的都是一段胁后,左移這個(gè)方法不是必須的
我們可以通過(guò)下面這個(gè)方法返回你想要在UITableView中的某一段中顯示的行數(shù)
- (NSInteger)tableView:(UITableView *)tView numberOfRowsInSection:(NSInteger)section;//通常情況下是一段嗦枢,所以不必判斷段數(shù)
假如您有很多段攀芯,而且每一個(gè)段的顯示行數(shù)還不一樣,你就要通過(guò)上面的方法精確控制文虏,例如:
- (NSInteger)tableView:(UITableView *)tView numberOfRowsInSection:(NSInteger)section {
if(section == 0) return 1;
if(section == 1) return 1;
if(section == 2) return 8;
if(section == 3) return 1;
if(section == 4 || section ==5) return 3;
if(section == 6)return 4;
return0;
}
這個(gè)方法時(shí)必須的侣诺。
另外我們可以通過(guò)下面這個(gè)方法精確控制某一行的高度
-
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0 && indexPath.row ==0) return80;
return 40.0;
}
另外我們可以通過(guò)下面這個(gè)方法精確控制某一段的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)return60.0;
return40.0;
}
另外我們還可以通過(guò)下面這個(gè)方法精確控制某一段的標(biāo)題和視圖
(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section氧秘;
(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section年鸳;