UITableView 的更多屬性


1.    UITableView的初始化
[csharp] 
UITableView tableview= [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)]; 
 [tableview setDelegate:self]; 
 [tableview setDataSource:self]; 
 [self.view addSubview: tableview]; 
 [tableview release];  app


(1)在初始化UITableView的時候必須實現UITableView的是,在.h文件中要繼承UITableViewDelegate和UITableViewDataSource,並實現3個UITableView數據源方法和設置它的delegate爲self,這個是在不直接繼承UITableViewController實現的方法。
(2) 直接在XCODE生成項目的時候繼承UITableViewController的,它會幫你自動寫好UITableView必需要實現的方法。
(3) UITableView繼承自UIScrollView。
2.    UITableView的數據源
(1) UITableView是依賴外部資源爲新表格單元填上內容的,咱們稱爲數據源,這個數據源能夠根據索引路徑提供表格單元格,在UITableView中,索引路徑是NSIndexPath的對象,能夠選擇分段或者分行,便是咱們編碼中的section和row。
(2) UITableView有三個必須實現的核心方法,分別以下:
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView;
這個方法能夠分段顯示或者單個列表顯示咱們的數據。以下,左邊爲分段顯示,右邊爲單個列表顯示: 函數

 \\



-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section;
這個方法返回每一個分段的行數,不一樣分段返回不一樣的行數能夠用switch來作,若是是單個列表就直接返回單個你想要的函數便可。
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath;
這個方法是返回咱們調用的每個單元格。經過咱們索引的路徑的section和row來肯定。
3.    UITableView的委託方法
使用委託是爲了響應用戶的交互動做,好比下拉更新數據和選擇某一行單元格,在UITableView中有很大這種方法供咱們選擇。
(1) 委託方法講解
[csharp]
//設置Section的數量 
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ 
 return TitleData; 

//設置每一個section顯示的Title 
- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{ 
 return @"Andy-清風"; 

 
//指定有多少個分區(Section),默認爲1 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
 return 2; 

 
//指定每一個分區中有多少行,默認爲1 
- (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section{ 

 
//設置每行調用的cell 
-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
   
    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier: 
                             SimpleTableIdentifier]; 
    if (cell == nil) {   
        cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier:SimpleTableIdentifier] autorelease]; 
 } 
 cell.imageView.image=image;//未選cell時的圖片 
 cell.imageView.highlightedImage=highlightImage;//選中cell後的圖片 
 cell.text=@」Andy-清風」; 
 return cell; 

//設置讓UITableView行縮進 
-(NSInteger)tableView:(UITableView *)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{ 
 NSUInteger row = [indexPath row]; 
 return row; 

//設置cell每行間隔的高度 
- (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    return 40; 

//返回當前所選cell 
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section]; 
[TopicsTable selectRowAtIndexPath:ip animated:YESscrollPosition:UITableViewScrollPositionNone]; 
 
//設置UITableView的style 
[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone]; 
//設置選中Cell的響應事件 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{ 
 
 [tableView deselectRowAtIndexPath:indexPath animated:YES];//選中後的反顯顏色即刻消失 

 
//設置選中的行所執行的動做 
 
-(NSIndexPath *)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath 

    NSUInteger row = [indexPath row]; 
     return indexPath; 

//設置划動cell是否出現del按鈕,可供刪除數據裏進行處理 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath { 

//設置刪除時編輯狀態 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath 

} 
 
//右側添加一個索引表 
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ 
學習


(2)  其餘
[csharp]
//選中cell時的顏色,在官方文檔有以下能夠選擇 
 
typedef enum { 
    UITableViewCellSelectionStyleNone, 
    UITableViewCellSelectionStyleBlue, 
    UITableViewCellSelectionStyleGray 
} UITableViewCellSelectionStyle 
 
  
 
//cell右邊按鈕格式 
 
typedef enum { 
    UITableViewCellAccessoryNone,                   //don't show any accessory view 
    UITableViewCellAccessoryDisclosureIndicator,    //regular chevron. doesn't track 
    UITableViewCellAccessoryDetailDisclosureButton, //blue button w/ chevron. tracks 
    UITableViewCellAccessoryCheckmark               //checkmark. doesn't track 
} UITableViewCellAccessoryType 
 
  
 
//是否加換行線 
 
typedef enum { 
    UITableViewCellSeparatorStyleNone, 
    UITableViewCellSeparatorStyleSingleLine 
} UITableViewCellSeparatorStyle 
 
  
 
//改變換行線顏色 
 
tableView.separatorColor= [UIColor blueColor];  編碼


 
4.    UITableViewCell
表中的每一行都表明一個UITableViewCell。可使用圖像、文本還有輔助的圖標等來自定義你本身的UITableViewCell。你能夠自定義你本身的cell以下模型或者像appstore那樣的。
\ spa

UITableViewCell爲每一個Cell提供了三個能夠選擇的屬性,以下:
l textLabel:填寫文本
l detailTextLable:稍微詳細的副標題
l imageView:用來顯示你cell的圖片,能夠經過UIImage來加載。
最後給出一個官方的demo給你們學習下,多實踐,不懂的就問下,下節課講些UITableView應用中實際會出現的問題,好比自定義啊,重用單元格,單元格的數據排序等問題。歡迎你們拍磚。 對象

相關文章
相關標籤/搜索