iPhone開發(fā)應用中關于UITableView詳細教程
iPhone開發(fā)應用中關于UITableView詳細教程是本文要介紹的內容,主要是來學習UITableView的詳細操作。UITableView是一個很強大的控件,在我們iphone開發(fā)過程中會經(jīng)常用到。
下面我做以下簡單介紹
UITableView有一個基本元素的索引NSIndexPath,你可以通過索引NSIndexPath找到UITableView下面的子元素只要這個方法
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- //在這個方法里你可以根據(jù)NSIndexPath判斷相應的元素,然后做處理
例如:
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if(indexPath.section == 5 && indexPath.row ==2)return;
- }
- UITableView的每一個子元素(Entity)稱為UITableViewCell,UITableViewCell由下面這個方法初始化
- -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
- //這個方法是必需的,他是產生UITableView內容的必須載體
例如:
- -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
- {//因為UITableView由很好的內存控制機制,他每次只加載一屏幕的cell(7個左右),當用戶觸摸移動時,動態(tài)加載新產生的cell
- static NSString *RootViewControllerCell =@"HelpViewControllerCell";//產生一個靜態(tài)標識
- UITableViewCell* cell =(UITableViewCell*)[tableViewdequeueReusableCellWithIdentifier:RootViewControllerCell];//標記新產生的cell
- if(cell==nil) //如果cell不為空標識cell還沒有被釋放,還在屏幕中顯示
- {
- cell =[[[UITableViewCellalloc]initWithStyle:UITableViewCellSeparatorStyleSingleLinereuseIdentifier:RootViewControllerCell]autorelease];
- }
- return cell;
- }
UITableViewCell通過NSIndexPath索引,正如上面所看到的,NSIndexPath索引由兩個屬性section和row
section通常就是我們所謂的段,row所謂的段內的行
我們可以通過下面這個方法返回你想要在UITableView中顯示段數(shù)
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; //通常情況下我們看到的都是一段,左移這個方法不是必須的
我們可以通過下面這個方法返回你想要在UITableView中的某一段中顯示的行數(shù)
- - (NSInteger)tableView:(UITableView *)tView numberOfRowsInSection:(NSInteger)section;//通常情況下是一段,所以不必判斷段數(shù)
假如您有很多段,而且每一個段的顯示行數(shù)還不一樣,你就要通過上面的方法精確控制,例如:
- - (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;
- }
這個方法時必須的。
另外我們可以通過下面這個方法精確控制某一行的高度
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- if(indexPath.section == 0 && indexPath.row ==0) return80;
- return 40.0;
- }
另外我們可以通過下面這個方法精確控制某一段的高度
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- {
- if (section == 0)return60.0;
- return40.0;
- }
另外我們還可以通過下面這個方法精確控制某一段的標題和視圖
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
這兩個方法鄙視必須的,我就不作介紹。
擁有以上的及格方法你就可以完成超炫的UITableView視圖了。前提是要活靈活用,還有其他幾個常用的控件,這里就不在寫了,需要的可以留言交流。
小結:iPhone開發(fā)應用中關于UITableView詳細教程的內容介紹完了,希望本文對你有所幫助!