iOS開發-UITableView 的使用 GeekBand

iPhone開發應用中關于UITableView詳細教程是本文要介紹的內容,主要是來學習UITableView的詳細操作。UITableView是一個很強大的控件,在我們iphone開發過程中會經常用到。
下面我做以下簡單介紹
?UITableView有一個基本元素的索引NSIndexPath,你可以通過索引NSIndexPath找到?UITableView下面的子元素只要這個方法

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    //在這個方法里你可以根據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個左右),當用戶觸摸移動時,動態加載新產生的cell
static NSString RootViewControllerCell =@"HelpViewControllerCell";//產生一個靜態標識
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中顯示段數

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; //通常情況下我們看到的都是一段,左移這個方法不是必須的

我們可以通過下面這個方法返回你想要在UITableView中的某一段中顯示的行數

  • (NSInteger)tableView:(UITableView *)tView numberOfRowsInSection:(NSInteger)section;//通常情況下是一段,所以不必判斷段數

假如您有很多段,而且每一個段的顯示行數還不一樣,你就要通過上面的方法精確控制,例如:

  • (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;

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容