IOS學習筆記(十二)之IOS開發之表視圖(UITableView)的相關類,屬性與表視圖實現學習(二)

來源:互聯網
上載者:User

IOS學習筆記(十二)之IOS開發之表視圖(UITableView)的講解與使用(二)(部落格地址:http://blog.csdn.net/developer_jiangqq)轉載請註明地址.

Author:hmjiangqq

Email:jiangqqlmj@163.com

上一篇初步學習了一下表視圖(UITableView)的基本概念內容(點擊進入),今天就表視圖的其他知識進行學習,並且實現表視圖。

(一)UITableView的相關類解析:

首先我們來看張類的結構圖:

1:表視圖(UITableView)是繼承自UIScrollView,這樣就可以使得我們的表視圖可以實現上下的滾動。

2:同時表視圖(UITableView),還有兩個委託①:UITableViewDelegate委託協議,一般我們用來處理表視圖的基本樣式(例如:單元格的高度等)還有去捕捉選中單元格的事件。②:UITableViewDataSource委託協議,必要要去實現該協議的資料來源方法,來完成表視圖的資料配置。

3:UITableViewController:是表視圖(UITableView)的控制器類。

4:UItableViewCell:是單元格類.


(二):資料來源協議和委託源協議介紹:

1:UITableViewDataSource協議:我們去實現其中的方法,來完成我們的表視圖的資料配置,從而來顯示表視圖。其中我們必須要去實現的兩個方法如下:

//進行返回每個section(節)中儲存格的數量- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)// 為表視圖中的儲存格建立資料- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
除了以上的兩個必須實現的方法,還有一些以下的可選實現方法:

// 返回section(節)的個數- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;              // Default is 1 if not implemented//返回section(節)頭的標題- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    // fixed font style. use custom view (UILabel) if you want something different// 返回section(節)尾的標題- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;                                                    // return list of section titles to display in section index view (e.g. "ABCD...Z#")- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
2:UITableViewDelegate:協議可以用來設定表視圖中的節頭與節尾 同時還可以去響應一些點擊事件,主要的一些方法如下:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;   // custom view for header. will be adjusted to default or specified header height- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;   // custom view for footer. will be adjusted to default or specified footer height- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
更多方法可以去官網UITableView進行查詢。
(三) 表視圖(UITableView)的一些常用方法和屬性:

1:常用屬性:

①:@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle; 預設為UITableViewCellSeparatorStyleSingleLine

②:@property(nonatomic,retain) UIColor *separatorColor; 預設為:the standard separator gray

③:@property(nonatomic,retain) UIView *tableHeaderView; 頭部視圖

④:@property(nonatomic,retain) UIView *tableFooterView; 尾部視圖

⑤:@property(nonatomic) CGFloat rowHeight; // 單元格高度

⑥:@property(nonatomic) CGFloat sectionHeaderHeight; // 頭部行高

⑦:@property(nonatomic) CGFloat sectionFooterHeight; //尾部行高

⑧:@property(nonatomic,readwrite,retain) UIView *backgroundViewNS_AVAILABLE_IOS(3_2);

⑨:@property(nonatomic,readonly) UITableViewStyle style;

2:常用方法:

①:- (void)reloadData; // reloads everything from scratch. redisplays visible rows. because we only keep info about visible rows, this is cheap. will adjust offset if table shrinks 重新整理單元格的資料

②:- (void)reloadSectionIndexTitlesNS_AVAILABLE_IOS(3_0); // reloads the index bar.

③:- (NSInteger)numberOfSections; //返回節的數量

④:- (NSInteger)numberOfRowsInSection:(NSInteger)section;//返回每個節的單元格的數量

⑤:- (CGRect)rectForSection:(NSInteger)section; // includes header, footer and all rows

⑥:- (CGRect)rectForHeaderInSection:(NSInteger)section;

⑦:- (CGRect)rectForFooterInSection:(NSInteger)section;

⑧:- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;

⑨:- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point; // returns nil if point is outside table

⑩:- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell; //返回指定單元格的NSIndexPath執行個體

十一:- (NSArray *)indexPathsForRowsInRect:(CGRect)rect; //返回指定範圍的NSIndexPath執行個體數組

十二:- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; // returns nil if cell is not visible or index path is out of range //返回指定NSIndexPath執行個體的單元格執行個體

十三:- (NSArray *)visibleCells; //返回可見的單元格的數組

十四- (NSArray *)indexPathsForVisibleRows; //返回可見單元格的NSIndexPath執行個體數組

十五:- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);

十六:- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)sectionNS_AVAILABLE_IOS(6_0);

十七:- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated; //滑動到指定的位置,並且可以加上動畫效果

十八:- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;


(四)例子實現表格布局

簡單的來說:是以下幾個步驟:1.配置資料來源,2.實現資料來源方法,3.設定代理方法。下面來看執行個體

////  ZTTRootViewController.m//  UITableViewDemo1////  Created by 江清清 on 14-3-19.//  Copyright (c) 2014年 江清清. All rights reserved.//#import "ZTTRootViewController.h"#import "ZTTDetailsViewController.h"#define  kDeviceHeight   [UIScreen mainScreen].bounds.size.height@interface ZTTRootViewController ()@end@implementation ZTTRootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        self.title=@"UITableView Style";    }    return self;}-(void)loadView{    UIView *view=[[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];    //[view setBackgroundColor:[UIColor redColor]];    self.view=view;    [view release];        //開始進行配置資料來源    self.listArray=@[@"UITableViewStylePlain",@"UITableViewStyleGrouped"];    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320,kDeviceHeight-20-44) style:UITableViewStylePlain];    //實現資料來源方法    [_tableView setDataSource:self];    //設定點擊事件 代理方法    [_tableView setDelegate:self];    [self.view addSubview:_tableView];    }- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark- tableview date source/* * 一個selection中又多少個儲存格 */- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return  [_listArray count];}// indexPath//建立儲存格- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *cellInditifier=nil;    // 建立儲存格對象    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellInditifier];    if(cell ==nil){        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInditifier]autorelease];    }    NSString *text=[self.listArray objectAtIndex:indexPath.row];    cell.textLabel.text=text;    return  cell;}// 表視圖中有幾個selection- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return  1;}// 選中儲存格的方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"didSelect");    //進行跳轉到相應的頁面    ZTTDetailsViewController *detailsVC=[[ZTTDetailsViewController alloc]init];    detailsVC.isPlain=indexPath.row==0?YES:NO;    [self.navigationController pushViewController:detailsVC animated:YES];    [detailsVC release];}-(void)dealloc{    [_tableView release];    _tableView=nil;    [super dealloc];}@end
運行如下:


3:上面的代碼例子是一般的表格,如果我們要表格中加入表頭(header)和表尾(footer)話,我們需要實現以下兩個資料來源方法:

<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; // fixed font style. use custom view (UILabel) if you want something different- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section; 運行如下:




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.