UITableView的基本用法

openkk 12年前發布 | 58K 次閱讀 iOS開發 移動開發 UITableView

幾乎大多數的IOS項目中都可以看得到UITableView 的影子,總結了一下,UITableView是iOS開發中,使用最廣泛的組件之一,通常都用它來展示一列數據 。開始看一下UITableView的基本語法:


一、UITableView有兩個代理協議
Protocol UITableViewDataSource:用來給TableView提供數據
Protocal UITableViewDelegate:控制TableView的展示方式以及事件響應
二、實現代理方法
 1、UITableViewDataSource代理方法實現
</span>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView //指定有多少個分區(Section),默認為1
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section//指定每個分區中有多少行,默認為1
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath //繪制Cell


示例代碼:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return1;//這里使用默認,如果你的數據需要分組顯示,在這里就可以定義你所需要的組的個數
}

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [arrays count];//arrays是你所定義的數據存儲數組 }

  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath { static NSString MyIdentifier = @"MyIdentifier"; //相當于一個行標識 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; //tableViewCell重繪 if (cell == nil) {

      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;
    

    } NSUInteger row = indexPath.row; //獲取行號 NSString *titleStr = [arrays objectAtIndex:row];//獲取數據 cell.textLabel.text = titleStr;//數據顯示 return cell;

}</pre>
當然這里還有一些復雜的使用,例如headerView 、 footerView  、titleForHeaderInSection  等等 。</span>

-(NSString)tableView:(UITableView)tableViewtitleForHeaderInSection:(NSInteger)section //設置分區高度

-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath //改變行的高度

-(NSInteger)tableView:(UITableView )tableView indentationLevelForRowAtIndexPath:(NSIndexPath )indexPath //行縮進</span></pre>
二、UITableViewDelegate的代理方法實現</span>

UITableViewDelegate用來管理Row的選擇和編輯,有四個方法如下:

tableView:willSelectRowAtIndexPath:
tableView:didSelectRowAtIndexPath:
tableView:willDeselectRowAtIndexPath:
tableView:didDeselectRowAtIndexPath:
此四個方法管理Row的選擇. 例如willSelectRowAtIndexPath, 如果此方法返回nil,那么所屬的row將無法被選中。

tableView:willBeginEditingRowAtIndexPath:
tableView:didEndEditingRowAtIndexPath:
tableView:editingStyleForRowAtIndexPath:
tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:
此四個方法在編輯Row時會被觸發。editingStyleForRowAtIndexPath決定Row是否可以被編輯,刪除或者移動。targetIndexPathForMoveFromRowAtIndexPath則在移動Row時會把觸發,在交換Row位置的時候,必須同時交換DataSource中數據的位置。
示例代碼:

//行縮進
-(NSInteger)tableView:(UITableView )tableView indentationLevelForRowAtIndexPath:(NSIndexPath )indexPath{ NSUInteger row = [indexPath row]; 
    return row;
} 
//改變行的高度

  • (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath{ return 40; }</pre>
    三、常用的一些枚舉類型選擇  

    //選中cell時的顏色 typedef enum {
      UITableViewCellSelectionStyleNone,
      UITableViewCellSelectionStyleBlue,
      UITableViewCellSelectionStyleGray
    } UITableViewCellSelectionStyle
    TIPS:自定義選中cell的背景顏色

     cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];
    cell.selectedBackgroundView.backgroundColor = [UIColor redColor];


    //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</pre>

//是否加換行線 typedef enum {
    UITableViewCellSeparatorStyleNone,
    UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle

//改變換行線顏色 tableView.separatorColor = [UIColor blueColor];

 

//系統提供的UITableView也包含了四種風格的布局  typedef enum {
    UITableViewCellStyleDefault, 
    UITableViewCellStyleValue1,
    UITableViewCellStyleValue2,
    UITableViewCellStyleSubtitle
} UITableViewCellStyle;

轉自:http://blog.csdn.net/zyc851224/article/details/7879262

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!