UITableView 協議

#import <UIKit/UIKit.h>

@interface VCRoot : UIViewController
<
//數據源協議
UITableViewDataSource,
//須要使用事件協議
UITableViewDelegate>
{
    UITableView*    _tableView ;
    NSMutableArray* _arrayData ;
}

@end

#import "VCRoot.h"

@interface VCRoot ()

@end

@implementation VCRoot

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped] ;
    
    _tableView.delegate = self ;
    _tableView.dataSource = self ;
    
    [self.view addSubview:_tableView] ;
    
    //建立數組
    _arrayData = [[NSMutableArray alloc] initWithCapacity:0] ;
    
    for (int i = 0 ; i < 5; i++)
    {
        NSString* str = [NSString stringWithFormat:@"第%d行",i+1] ;
        
        [_arrayData addObject:str] ;
    }
    [_tableView release] ;
    _tableView.backgroundColor = [UIColor clearColor];
    //[_arrayData release] ;
}
//析構函數
-(void) dealloc
{
    [_arrayData release] ;
    [super dealloc] ;
}
//組數
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 10 ;
}
//單元格行數
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrayData.count ;
}

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* strID = @"ID" ;
    //使用單元格複用機制建立cell對象
    UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:strID] ;
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strID] ;
    }
    
    NSString* str = [NSString stringWithFormat:@"第%d組,%@",indexPath.section+1,_arrayData[indexPath.row]];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.text =  str;
    
    return cell ;
}

//設置單元格的高度
//參數一:數據視圖
//參數二:單元格位置索引
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40 ;
    if (indexPath.row % 2 == 0) {
        return 40 ;
     }
    return 100 ;
}

//返回每組尾部的高度
//參數一:數據視圖
//參數二:當前的第幾組數
//默認值爲40
-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 40 ;
}

//組與組之間的空隙
//返回每組的頭部高度
//參數一:數據視圖
//參數二:當前的第幾組數
//默認值爲40
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 80 ;
}

//獲取每組頭部的文字信息
//參數一:tableview
//參數二:對應的組數
-(NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString* str = [NSString stringWithFormat:@"第%d組頭部",section+1] ;
    
    return str ;
}

//獲取每組尾部的文字信息
//參數一:tableview
//參數二:對應的組數
-(NSString*) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    NSString* str = [NSString stringWithFormat:@"第%d組結束",section+1] ;
    return str ;
}

//獲取索引列表數據數組
-(NSArray*) sectionIndexTitlesForTableView:(UITableView *)tableView
{
    //建立一個新數組,
    //用來保存右側小索引圖標的文字
    NSMutableArray* arrayIndex = [[NSMutableArray alloc] init] ;
    
    [arrayIndex addObject:@"開始"] ;
    
    for (int i = 'A' ; i < 'I' ; i++)
    {
        NSString* str = [NSString stringWithFormat:@"%c",i] ;
        
        [arrayIndex addObject:str] ;
    }
    [arrayIndex addObject:@"#"] ;
    _tableView.sectionIndexBackgroundColor = [UIColor clearColor];//索引條背景色
    //延遲釋放
    return [arrayIndex autorelease] ;
}

//點擊索引圖標文字時,跳轉到的位置
//返回值:跳轉到的組的索引section
//參數一:數據視圖
//參數二:點擊文字的字符串內容
//參數三:點擊的文字所在數組中的索引
-(NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    if ([title isEqualToString:@"開始"] == YES) {
        return -1 ;
    }
    
    if ([title isEqualToString:@"結束"] == YES) {
        return 9 ;
    }
    
    return index-1 ;
    
}

@end
相關文章
相關標籤/搜索