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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

IOS 程序猿 UITbleView 篇

發布時間:2024/4/14 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS 程序猿 UITbleView 篇 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

UITableView

在iOS中顯示數據列表最常用的一個控件,表格控件UITableView,它支持垂直滾動

它內置了兩種樣式:UITableViewStylePlain(平鋪),UITableViewStyleGrouped(分組)

那么TableView是怎么展示數據的呢?

UITableView需要一個數據源(dataSource)來顯示數據,UITableView會向數據源查詢一共有多少行數據以及每一行顯示什么數據等。沒有設置數據

源的UITableView只是個空殼。凡是遵守UITableViewDataSource協議的OC對象,都可以是UITableView的數據源通常都要為UITableView設置代

理對象(delegate),以便在UITableView觸發一下事件時做出相應的處理,比如選中了某一行。凡是遵守了UITableViewDelegate協議的OC對象,都

可以是UITableView的代理對象,一般會讓控制器充當UITableView的dataSource和delegate。

?廢話不多說 看下面 ↓

<UITableViewDataSource,UITableViewDelegate>的方法:

//有多少組

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

//第section組頭部控件有多高

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

//第section組有多少行

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

//indexPath這行的cell有多高

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

//indexPath這行的cell長什么樣子

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

//第section組頭部顯示什么控件

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

//當調用reloadData的時候,會重新刷新調用數據源內所有方法,其他事情都不會做

?[self reloadData]

?

?//這個方法只有在一開始有多少條數據會算多少個高度,這個方法只會調用一次,但是每次reloadData的時候也會調用

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

?-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView //右側索引

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath //行點擊事件

?NSIndexPath *path = [self.tableView indexPathForSelectedRow]; //獲得被選中的indexPath可以得到section,row

[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationNone]; //刷新table指定行的數據

?[self.tableView reloadData]; //刷新table所有行的數據

?

UITableView常用屬性:

// 初始化表格(并設置位置,寬高,樣式)。

UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];

??? 分隔線屬性

tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; //UITableViewCellSeparatorStyleNone;

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; //取消分隔線

tableView.separatorColor = [UIColor lightGrayColor];

???

? ?// 條目多選

tableView.allowsMultipleSelection = YES;

????

??? // 設置標題行高

[_tableView setSectionHeaderHeight:kHeaderHeight];

[_tableView setSectionFooterHeight:0];

?

??? // 設置表格行高

?[_tableView setRowHeight:50];

?

//設置背景色

self.tableView.backgroundView? 優先級高,如果要設置backgroundColor的時候要先把view設置為nil

self.tableView.backgroundColor = [UIColor redColor];

?

//在tableView的頭部或者尾部添加view,footerView寬度是不用設置的

?????? xxxView.bounds = CGRectMake(0,0,0,height);

???  self.tableView.tableFooterView =xxxView;

?????? self.tableView.tableHeaderView =xxxView;

?

增加tableview滾動區域

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, xx, 0);?

UITableViewCell

/* lUITableView的每一行都是一個UITableViewCell,通過dataSource的tableView:cellForRowAtIndexPath:方法來初始化每一行 UITableViewCell是UIView的子類,內部有個默認的子視圖:contentView。contentView是UITableViewCell所顯示內容的父視圖,并負責顯示一些輔助指示視圖。輔助指示視圖的作用是顯示一個表示動作的圖標,可以通過設置UITableViewCell的accessoryType來顯示,默認是UITableViewCellAccessoryNone */

//創建UITableViewCell????????

UITableViewCell *cell = [[UITableViewCell alloc]

initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

[cell.textLabel setBackgroundColor:[UIColor clearColor]];// 清空標簽背景顏色

cell.backgroundView =xx; //設置背景圖片

cell.backgroundVColor =xx;//設置背景顏色

cell.selectedBackgroundView = selectedBgView; //設置選中時的背景顏色

cell.accessoryView = xxxView; //設置右邊視圖

[cell setAccessoryType:UITableViewCellAccessoryNone]; //設置右側箭頭?

[self setSelectionStyle:UITableViewCellSelectionStyleNone]; //選中樣式

cell.selectionStyle = UITableViewCellSelectionStyleBlue;

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath//設置cell的高度

?

/*

contentView下默認有3個子視圖,其中的2個是UILabel,通過textLabel和detailTextLabel屬性訪問,第3個是UIImageView,通過imageView屬性訪問.

UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle

?*/

#pragma mark - 重新調整UITalbleViewCell中的控件布局

- (void)layoutSubviews

{

  [super layoutSubviews];

}

cell 里面還有一個contentView

UITableViewCell表格優化

UITableViewCell對象的重用原理:

liOS設備的內存有限,如果用UITableView顯示成千上萬條數據,就需要成千上萬個UITableViewCell對象的話,那將會耗盡iOS設備的內存。要解決 該問題,需要重用UITableViewCell對象 重用原理:當滾動列表時,部分UITableViewCell會移出窗口,UITableView會將窗口外的UITableViewCell放入一個對象池中,等待重用。當 UITableView要求dataSource返回UITableViewCell時,dataSource會先查看這個對象池,如果池中有未使用的UITableViewCell,dataSource會 用新的數據配置這個UITableViewCell,然后返回給UITableView,重新顯示到窗口中,從而避免創建新對象 l還有一個非常重要的問題:有時候需要自定義UITableViewCell(用一個子類繼承UITableViewCell),而且每一行用的不一定是同一種 UITableViewCell(如短信聊天布局),所以一個UITableView可能擁有不同類型的UITableViewCell,對象池中也會有很多不同類型的 UITableViewCell,那么UITableView在重用UITableViewCell時可能會得到錯誤類型的UITableViewCell 解決方案:UITableViewCell有個NSString *reuseIdentifier屬性,可以在初始化UITableViewCell的時候傳入一個特定的字符串標識來設置 reuseIdentifier(一般用UITableViewCell的類名)。當UITableView要求dataSource返回UITableViewCell時,先通過一個字符串標識到對象池中查 找對應類型的UITableViewCell對象,如果有,就重用,如果沒有,就傳入這個字符串標識來初始化一個UITableViewCell對象

?

單元格優化

/*

?1. 標示符統一,使用static的目的可以保證表格標示符永遠只有一個

?2. 首先在緩沖池中找名為"myCell"的單元格對象

?3. 如果沒有找到,實例化一個新的cell

*/

?

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

{

???  static NSString *cellIdentifier = @"myCell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

   //使用這種方法不用判斷下面的cell

???  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

???  if (cell == nil) {???????

???????    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

  ? ?}

   return cell;

}

?

表格的編輯模式

刪除、插入

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;? 開啟表格編輯狀態

?

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

? ? ? ?// 返回表格編輯編輯樣式。不實現默認都是刪除

  return editingStyle : UITableViewCellEditingStyleDelete, UITableViewCellEditingStyleInsert

}

?

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

??? ??// 根據editingStyle處理是刪除還是添加操作完成刪除、插入操作刷新表格

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

?

-(void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

}

?

移動

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

sourceIndexPath 移動的行????

destinationIndexPath 目標的行

?

自定義表格行UITableViewCell

storyboard方式創建:

直接拖到UITableView里面設置UITableViewCell

注意:

1.通過XIB或者Storyboard自定義單元格時,在xib和Storyboard里面需要指定單元格的可重用標示符Identifier

?

2.注意表格的優化中的差別

在Storyboard中兩者等效

xxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

xxCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

?

在xib文件中有差別:

第一種情況,只能在iOS 6以上使用,如果在viewDidLoad注冊了nib文件,并且指定了“單元格”的可重用標示符,那么????

???? dequeueReusableCellWithIdentifier:

???? dequeueReusableCellWithIdentifier:forIndexPath:

???? 方法是等效的。如果在viewDidLoad中注冊了nib文件,表格緩沖池中的管理,有系統接管!

?

第二種情況,是在iOS 4以上均可以使用,如果沒有在viewDidLoad注冊nib文件,那么,只能使用

???? dequeueReusableCellWithIdentifier:并且需要判斷cell沒有被實例化,并做相應的處理

?

?

在代碼創建中差別:

用代碼創建cell中的處理和nib一樣,注冊了cell就有系統接管并且可以用帶forIndexPath的方法,沒有注冊就要自己去實例化cell,不能用帶forIndexPath的方法

[tableView registerClass:XxxCell class] forCellReuseIdentifier:@"xxCell"];

?

xib方式創建:

// 注冊Identifier

- (void)viewDidLoad{

??? [super viewDidLoad];

??? /**

???? 注意:以下幾句注冊XIB的代碼,一定要在viewDidLoad中!

???? 注冊XIB文件,獲得根視圖,并且轉換成TableView,為tableView注冊xib

???? Identifier名要在xib文件中定義,并且保持一致

???? **/

??? UINib *nib = [UINib nibWithNibName:@"BookCell" bundle:[NSBundle mainBundle]];

??? UITableView *tableView = (UITableView *)self.view;

??? [tableView registerNib:nib forCellReuseIdentifier:@"bookCell"];???

}

?

// 沒有注冊Identifier只能使用下面方法

static NSString *CellIdentifier = @"xxxCell";

XxxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

??????? cell = [[XxxCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

??????? NSBundle *bundle = [NSBundle mainBundle];

??????? NSArray *array = [bundle loadNibNamed:@"XxxCell" owner:nil options:nil];

??????? cell = [array lastObject];

}

?

?


?

?

轉載于:https://www.cnblogs.com/blackCat-tommy/p/3572721.html

總結

以上是生活随笔為你收集整理的IOS 程序猿 UITbleView 篇的全部內容,希望文章能夠幫你解決所遇到的問題。

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