UITableView表視圖簡單使用


一、表視圖的使用場景

表視圖 UITableView,繼承自UIScrollView,所以可以滾動,iOS中最重要的視圖,隨處可見。

表視圖通常用來管理一組具有相同數據結構的數據。

表視圖的每一條數據都是顯示在UITableViewCell對象中

   表視圖可以分區顯示數據,每個分區稱爲一個section,每一行稱爲row,編號都是從0開始

二、表視圖的創建及顯示數據

先介紹下UITableView得相關屬性:

重要屬性

style樣式

plain

group

分割線樣式

separatorStyle

分割線顏色

separatorColor

行高

rowHeight


下面是部分創建UITableView的代碼:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationController.navigationBar.translucent = NO;

    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    tableView.rowHeight = 40;
    tableView.separatorColor = [UIColor redColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [lb setText:@"You are my music King"];
    [lb setBackgroundColor:[UIColor greenColor]];
    tableView.tableHeaderView = lb;

    //UITableView的兩套協議
    //tableView的delegate的方法如非必要,儘量不要實現
    tableView.delegate = self;
    NSLog(@"%@",tableView.delegate);
    NSLog(@"tableView  delegate%s === %d",__func__,__LINE__);
    tableView.dataSource = self;
    NSLog(@"%@",tableView.dataSource);
    NSLog(@"tableView dataSource  === %s  %d",__FUNCTION__,__LINE__);


    [self.view addSubview:tableView];
    [tableView release];
}


三、表視圖的重用機制

TableView的數據源UITableViewDataSource

DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;



創建多個分區方法:

DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 

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


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; 


TableView的委託UITableViewDelegate

Delegate

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;


如果當前類是繼承自UIViewController,需要添加上面的代碼,如果直接繼承自UITableViewController則不需要添加。

自定義區頭區尾

重要屬性

設置圖片

imageView

設置文本

textLabel

指定選中效果

selectionStyle

指定輔助效果樣式

accessoryType

選中背景圖

        selectedBackgroundView


自定義區頭區尾方法:

Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

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

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;


四、表視圖的相關配置方法

NSIndexPath

           是Foundation框架中的一個普通的類,它提供了到嵌套數列的樹中特定節點的路徑,事實上,它是一個整數陣列,表格視圖使用這個去表現在特定章節中的特定行,UITableView用的所有索引路徑正好有兩個元素,第一個是章節,第二個是行。

重要屬性

NSIndexPath


row

section

+(NSIndexPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section


//NSIndexPath和TableViews
@interfaceNSIndexPath (UITableView) {
}
+(NSIndexPath*)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;
@property(nonatomic,readonly)NSUIntegerrow;
@property(nonatomic,readonly)NSUIntegersection;
@end

SingleSection Table View
//返回行數
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return[myStringcount];
}
//請求時提供一個單元
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
UITableViewCell*cell = ......;
cell.textLabel.text=[myStringobjectAtIndex:indexPath.row];
return[cell autorelease];
}
相關方法實現:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 3;
    } else {
        return 2;
    }

    return [_tableArray count];
}
//將要出現cell的時候執行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //聲明僞靜態字符串來標示重用池,目的是我們不需要管理內存。
    static NSString *cellIdentity = @"_cell";
    //在重用池中把cell取出來
    CustomTableViewCell *cell = [[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentity];
    if (!cell) {
        //創建一個cell到重用池裏面,方便下次調用
        cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.showsReorderControl = YES;
    }
    //[cell.textLabel setText:@"黑貓"];
    cell.imageView.image = [UIImage imageNamed:@"10.jpeg"];
    cell.detailTextLabel.text = @"First Love";
    NSLog(@"cell ==== %@",cell);
//    NSLog(@"section == %d  row ==  %d",indexPath.section,indexPath.row);
    NSString *str = [_tableArray objectAtIndex:indexPath.row];
    [cell.textLabel setText:str];
    return cell;
}
#pragma mark 指定有多少個分區(Section),默認爲1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;{
    return 4;
}

#pragma mark 設置每個分組的標題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"那些年我們一起聽過的情歌";
}
#pragma mark 設置每個行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 40;
        } else if (indexPath.row == 1)
        {
            return 80;
        }
        else if (indexPath.row == 2){
            return 100;
        }
    }
    return 70;
}
#pragma mark 標示圖Section頭的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

#pragma mark 標示圖Section尾的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 30;
}
//Section總數
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    NSMutableArray *titleArray = [NSMutableArray array];
    for (int i = 65; i <= 90; i++) {
        [titleArray addObject:[NSString stringWithFormat:@"%c", i]];
    }
    return titleArray;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章