日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IOS开发之表视图(UITableView)

發布時間:2024/4/13 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS开发之表视图(UITableView) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

IOS開發之表視圖(UITableView)的基本介紹(一)

(一):UITableView的基本概念

?

? ? 1.在IOS開發中,表視圖的應用十分廣泛和普及。因此掌握表視圖的用法顯得非常重要。一般情況下對于數據的展示

我們都會選擇表視圖,比如通訊錄和一些數據列表。

? ? 2.我們可以選擇創建表視圖也可以創建表視圖控制器。

(二)UITableView基本樣式如下(1:UITableViewStylePlain(普通表視圖),2:UITableViewStyleGroup(分組表視圖)):

(三)UITableView表視圖的結構:首先我們來看一張設計圖:

? ? ?

?根據上面的圖,下面我們來分析一下表視圖的結構:

1:表頭視圖(table header view).表視圖最上邊的視圖,用于展示表視圖的信息,例如上面下拉刷新信息。

2:表腳視圖(table footer view).表視圖最下邊的視圖,用于展示表視圖的部分信息,例如上圖加載更多信息。

3:單元格(cell)。它是組成表視圖每一行的單位視圖,上圖一行一行的單元圖

4:節(selection)。它是多個單元格在組成,并且有節頭和節腳,正如上圖藍色框框的(Group Start)節頭,(Group End)節尾

5:節頭。節的頭部,描述節的信息,例如上圖 Group Start.

6:節腳.節的尾部,描述節的信息或者一些聲明信息。例如上圖 Group End.

(四)UITableView表視圖的分類

? ? ?IOS中表視圖主要分為兩大類(普通表視圖與分組表視圖),下面來稍微了解一下這兩類表視圖

? ? ?1:普通表視圖:主要用于一般表,最常見的是,我們不知道有多少數據需要進行顯示.

? ? ?2:分組表視圖:主要用于都一些數據進行布局分成不同的組進行顯示。見下面的圖,左邊為普通表視圖,右邊為分組表視圖;

? ??? ? ?

? ?除此之外,看上面左邊的圖是帶上索引列的(efghijk...),同樣還可以加上選擇列和搜索欄。

? ?3:下圖,給用戶提供一個選擇的列表,這樣使用選擇表視圖可以代替復選框控件。

? ??

? ? ?下面帶了搜索欄如:這樣可以在我單元格很多的情況之下,去借助搜索欄進行過濾搜索。搜索欄一般都會放在表頭。

需要我們把UITableView上滑倒最頂端才能看到搜索欄。

? ? ?

以上就是先介紹一下表視圖(UITableView)包括表視圖的基本概念,基本樣式,結構以及表視圖的分類,下一篇就開始實現表視圖并進行深入理解。

?


?

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

?(一)UITableView的相關類解析:

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

? ??

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

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

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

? ?4:UItableViewCell:是單元格類.

?

(二):數據源協議和委托源協議介紹:

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

?

[objc]?view plaincopy
  • <span?style="font-size:18px;">//進行返回每個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;</span>??
  • ? 除了以上的兩個必須實現的方法,還有一些以下的可選實現方法:

    ?

    ?

    [objc]?view plaincopy
  • <span?style="font-size:18px;">//?返回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;??
  • </span>??
  • ? ? ?2:UITableViewDelegate:協議可以用來設定表視圖中的節頭與節尾 同時還可以去響應一些點擊事件,主要的一些方法如下:

    ?

    ?

    [objc]?view plaincopy
  • <span?style="font-size:18px;">-?(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;</span>??
  • ? 更多方法可以去官網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.設置代理方法。下面來看實例

    ? ? ??

    [objc]?view plaincopy
  • <span?style="font-size:18px;">//??
  • //??ZTTRootViewController.m??
  • //??UITableViewDemo1??
  • //??
  • //??Created?by?江清清?on?14-3-19.??
  • //??Copyright?(c)?2014年?江清清<<a>http://www.0513.it/</a>中天科技軟件技術股份有限公司>.?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??
  • </span>??
  • 運行截圖如下:

    ?

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

    ?

    [objc]?view plaincopy
  • <span?style="font-size:18px;">-?(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;</span>??
  • ? ? ?運行截圖如下:

    ?

    ??

    ?

    總結

    以上是生活随笔為你收集整理的IOS开发之表视图(UITableView)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。