UITableView 常用的委託方法:

1 分區的個數


- (NSInteger)numberOfSectionsInTableView:(UITableView *) tableView
{
    return  數據分組的個數; 
    //例 return 2:

}
 

2 屏幕右邊的標題索引


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSArray *temp=[NSArray arrayWithObjects:@"xigua",@"tudou",nil];
    return temp;
}
 

3 點擊單元格出發的事件


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     //點擊的事件

}
 

4 點擊刪除按鈕時,能夠刪除,指定位置的單元格,和對應的數組中的內容


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{


}

 

5 每個分區的行數


- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{

}
 

6 每行 的縮進


- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{

}

 

7 構建單元格


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             SimpleTableIdentifier];
    if (cell == nil) {      
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier: SimpleTableIdentifier] autorelease];

        cell.imageView.image=[UIImage imageNamed:@"flake"];
        cell.backgroundView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"star.png"]];
}
 

8 設置分區的 每一個標題


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    if (section==0) {
        return @"luobo";
    }
    return @"tudou";

}
 

9 爲每個分區設置一個按鈕(視圖)


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIButton *sectionbutton=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    sectionbutton.frame=CGRectMake(0, 0, 320, 30);

    if (section==0) {

        [sectionbutton setTitle:@"xigua" forState:UIControlStateNormal];

    }

    if(section==1)
    {
        [sectionbutton setTitle:@"tudou" forState:UIControlStateNormal];

    }

    sectionbutton.tag=section;

    [sectionbutton addTarget:self action:@selector(titlebutton:) forControlEvents:UIControlEventTouchUpInside];

    return sectionbutton;


}
 

10 會在點擊cell右邊的指示button時,自動調用本方法。


- (void)tableView:(UITableView *)tableView 
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    //是爲了防止本界面頻繁的進入下一個界面時,頻繁的創建下一個界面的對象。
    //重複使用childController。
    //因爲是數組保存了控制器對象,所控制器的對象始終爲1,不能釋放,
    //導致下一個控制器對象調用的控制器對象創建以後就不會消失。
    if (childController == nil)
        childController = [[DisclosureDetailController alloc] 
                           initWithNibName:@"DisclosureDetail" bundle:nil];

    NSLog(@"111111");

   // childController.title = @"Disclosure Button Pressed";

    NSUInteger row = [indexPath row];

    NSString *selectedMovie = [list objectAtIndex:row];
    NSString *detailMessage  = [[NSString alloc] 
                                initWithFormat:@"You pressed the disclosure button for %@.", 
                                selectedMovie];
    childController.message = detailMessage;//因爲message屬性是retain
    [detailMessage release];
    //不能用下面的傳值方式,不安全,因爲在沒有xib的情況下,不能保證label不爲空
    //childController.label.text=detailMessage;
    childController.title = selectedMovie;


    [self.navigationController pushViewController:childController animated:YES];
}

11 不要該方法默認爲刪除風格


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;//沒有風格 
      //這是一個枚舉 還有其它風格
}

12 此方法可以沒有,默認返回yes,改爲no將無法移動


- (BOOL)tableView:(UITableView *)tableView 
canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

13表格的移動此方法可以實現在拖動單元格後,更改數據元的方法實現


- (void)tableView:(UITableView *)tableView 
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath 
      toIndexPath:(NSIndexPath *)toIndexPath {
    NSUInteger fromRow = [fromIndexPath row];
    NSUInteger toRow = [toIndexPath row];

    id object = [[list objectAtIndex:fromRow] retain];//
    [list removeObjectAtIndex:fromRow];//remove 只是讓其引用計數減一
    [list insertObject:object atIndex:toRow];
    [object release];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章