UITableView代理協議總結,uitableview代理協議

來源:互聯網
上載者:User

UITableView代理協議總結,uitableview代理協議
UITableView代理協議總結
1./** 一共有多少組 */-  (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.personGroups.count;
}

2./** 每一組有多少行 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    SUNPersonGroupInfo *personGroupInfo = self.personGroups[section];
    return personGroupInfo.persons.count;
}

3./** 顯示每一行的內容 */-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //由於cell的複用機制,會先從自動釋放池中找有沒有閒置名字相同的cell,如果有就複用,沒有就建立新的,
    static NSString*cellName=@"cell";
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:cellName];
    if(cell==nil){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
    }
    //由於cell的複用機制,需要把cell的設定全部清空,不然下面的複用的時候會出錯。
    cell.textLabel.text=nil;
    cell.detailTextLabel.text=nil;
    cell.accessoryType=UITableViewCellAccessoryNone;
    //分割線的設定
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.textLabel.font=nil;
    
    if(indexPath.section==0){
        //設定cell的主標題和副標題(cell的style不一樣,設定的效果也不一樣,有的還沒有副標題)
        cell.textLabel.text=@"主標題";
        cell.detailTextLabel.text=@"副標題";
        //設定cell右邊附屬按鈕的式樣,只有下面這個樣式可以點擊,其他樣式都不能點擊
        cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
        cell.selectionStyle=UITableViewCellSelectionStyleBlue;
        cell.textLabel.font=[UIFont systemFontOfSize:20];
        cell.imageView.image=[UIImage imageNamed:@"001_1"];
        //cell右邊視圖附屬視圖
        UIButton*btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:@"有嗎" forState:UIControlStateNormal];
        btn.frame=CGRectMake(0, 0, 50, 30);
        cell.accessoryView=btn;
        //選中後的顏色又不發生改變,進行下面的設定
        //cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //不需要分割線
        //tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    }else{
        cell.textLabel.text=@"第二個cell";
    }
    return cell;
}
注意:返回指定的row 的cell。這個地方是比較關鍵的地方,一般在這個地方來定製各種個人化的 cell元素。這裡只是使用最簡單最基本的cell 類型。其中有一個主標題 cell.textLabel 還有一個副標題cell.detailTextLabel,  還有一個 image在最前頭 叫cell.imageView.  還可以設定右邊的表徵圖,通過cell.accessoryType 可以設定是飽滿的向右的藍色箭頭,還是單薄的向右箭頭,還是勾勾標記。

4.//這個方法返回指定的 row 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

5.//這個方法返回指定的 section的header view 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
6.// 這個方法返回指定的 section的footer view 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
7./** 返回標題文字 */- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    SUNPersonGroupInfo *personGroup = self.personGroups[section];
    return personGroup.title;
}

8./** 右側索引 */
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [self.personGroups valueForKey:@"title"];
}

//開始編輯,一旦editing == YES就預設開啟刪除模式如果 self.tableView.editing= NO;
9.// 編輯模式- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleInsert;
} UITableViewCellEditingStyleNone,
 UITableViewCellEditingStyleDelete,     刪除
 UITableViewCellEditingStyleInsert      添加
10.// 可以顯示拖動控制項- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}11.//當使用者選中某個行的cell的時候,回調用這個。但是首先,必須設定tableview的一個屬性為可以select 才行。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"第%d個section中第%d行的被點擊",indexPath.section,indexPath.row);
}
注意:如果不希望響應select,那麼就可以用下面的代碼設定屬性:
TableView.allowsSelection=NO;

12.//如何設定tableview  每行之間的 分割線 如果不需要分割線,那麼就設定屬性為 UITableViewCellSeparatorStyleNone  即可。
self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;

13.//如何設定 tableview cell的背景顏色
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //設定背景顏色
    cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];
}

//這個函數響應,使用者點擊cell 右邊的 箭頭(如果有的話)
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

//如何設定tableview 可以被編輯,首先要進入編輯模式:如果要退出編輯模式,肯定就是設定為NO
[TableView setEditing:YES animated:YES];

//返回當前cell  要執行的是哪種編輯,下面的代碼是 返回 刪除  模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath


//通知告訴使用者編輯了 哪個cell,對應上面的代碼,我們在這個函數裡面執行刪除cell的操作。
-(void) tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

2.//滑動刪除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleInsert) {  
    }
}

3.//刪除的時候,左邊顯示的按鈕,預設是delegate;-(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"刪除";
}
注意:預設有一個UITableView,並且self.tableview == self.view使用以下兩條語句可以跟蹤驗證
NSLog(@"%p %p", self.view, self.tableView);
NSLog(@"%@", self.view.class);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.