欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解iOS App中UITableView的創(chuàng)建與內(nèi)容刷新

 更新時(shí)間:2016年04月24日 09:25:36   作者:淡淡微笑  
這篇文章主要介紹了iOS App中UITableView的創(chuàng)建與內(nèi)容刷新,講解了UITableView一些基本的樣式與cell的設(shè)置及刷新,需要的朋友可以參考下

UITableView幾乎是iOS開發(fā)中用處最廣的一個(gè)控件,當(dāng)然也是要記相當(dāng)多東西的一個(gè)控件。

創(chuàng)建
首先創(chuàng)建一個(gè)新的項(xiàng)目,并添加一個(gè)MainViewController的Class文件

201642491802522.png (214×66)

打開MainViewController.h文件

@interface MainViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> 
 
@property (nonatomic, retain) NSArray *dataList; 
@property (nonatomic, retain) UITableView *myTableView; 
 
@end 

TableView的數(shù)據(jù)源UITableViewDataSource。
TableView的委托UITableViewDelegate。
如果當(dāng)前類是繼承自UIViewController,需要添加上面的代碼,如果直接繼承自UITableViewController則不需要添加
然后打MainViewController.m文件,初始化UItableView并顯示在當(dāng)前窗口

- (void)viewDidLoad 
{ 
 [super viewDidLoad]; 
 // 初始化tableView的數(shù)據(jù) 
 NSArray *list = [NSArray arrayWithObjects:@"武漢",@"上海",@"北京",@"深圳",@"廣州",@"重慶",@"香港",@"臺(tái)海",@"天津", nil]; 
 self.dataList = list; 
  
 UITableView *tableView = [[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain] autorelease]; 
 // 設(shè)置tableView的數(shù)據(jù)源 
 tableView.dataSource = self; 
 // 設(shè)置tableView的委托 
 tableView.delegate = self; 
 // 設(shè)置tableView的背景圖 
 tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Background.png"]]; 
 self.myTableView = tableView; 
 [self.view addSubview:myTableView]; 
} 

在初始化的時(shí)候,可以為TableView設(shè)置樣式
第一種:列表 UITableViewStylePlain

201642491911192.png (329×491)

第二種:分組UITableViewStyleGrouped

201642491955065.png (330×493)

創(chuàng)建并設(shè)置每行顯示的內(nèi)容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
 static NSString *CellWithIdentifier = @"Cell"; 
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier]; 
 if (cell == nil) { 
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellWithIdentifier]; 
 } 
 NSUInteger row = [indexPath row]; 
 cell.textLabel.text = [self.dataList objectAtIndex:row]; 
 cell.imageView.image = [UIImage imageNamed:@"green.png"]; 
 cell.detailTextLabel.text = @"詳細(xì)信息"; 
 return cell; 
} 

UITableViewCell的樣式也是可以進(jìn)行設(shè)置的,如果不能滿足項(xiàng)目的需要,可以自己定義UITableViewCell的樣式
UITableViewCellStyleDefault

201642492019623.png (330×492)

UITableViewCellStyleSubtitle

201642492040034.png (331×491)

UITableViewCellStyleValue1

201642492059266.png (325×492)

UITableViewCellStyleValue2

201642492116014.png (328×489)

分組的TableView還可以進(jìn)行內(nèi)容的分段,是通過下面的方法實(shí)現(xiàn),返回的數(shù)字1代表分為1段

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
 return 1; 
} 

設(shè)置內(nèi)容縮進(jìn)

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
 return [indexPath row]; 
} 

201642492158931.png (324×490)

設(shè)置cell的行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
 return 70; 
} 

設(shè)置cell的隔行換色

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
 if ([indexPath row] % 2 == 0) { 
  cell.backgroundColor = [UIColor blueColor]; 
 } else { 
  cell.backgroundColor = [UIColor greenColor]; 
 } 
} 

201642492228564.png (333×494)

當(dāng)選擇指定的cell時(shí),彈出UIAlertView顯示選擇的內(nèi)容

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
  
 NSString *msg = [[NSString alloc] initWithFormat:@"你選擇的是:%@",[self.dataList objectAtIndex:[indexPath row]]]; 
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil]; 
 [msg release]; 
 [alert show]; 
} 

201642492258056.png (331×492)

滑動(dòng)選擇的行后刪除

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
 NSLog(@"執(zhí)行刪除操作"); 
} 

201642492330091.png (321×71)

UITableView的刷新:

[self.tableView reloadData];

reloadData是刷新整個(gè)UITableView,有時(shí)候,我們可能需要局部刷新。比如:只刷新一個(gè)cell、只刷新一個(gè)section等等。這個(gè)時(shí)候在調(diào)用reloadData方法,雖然用戶看不出來,但是有些浪費(fèi)資源。

刷新局部cell:

復(fù)制代碼 代碼如下:

 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
 [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];

這樣就可以很方便的刷新第一個(gè)section的第一個(gè)cell。雖然看起來代碼多了,但是確實(shí)比較節(jié)省資源。盡量少的刷新,也是UITableView的一種優(yōu)化。

局部刷新section:

NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];

上面這段代碼是刷新第0個(gè)section。

刷新動(dòng)畫:

刷新UITableView還有幾個(gè)動(dòng)畫:

typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
 UITableViewRowAnimationFade, //淡入淡出
 UITableViewRowAnimationRight, //從右滑入   // slide in from right (or out to right)
 UITableViewRowAnimationLeft, //從左滑入
 UITableViewRowAnimationTop,  //從上滑入
 UITableViewRowAnimationBottom, //從下滑入
 UITableViewRowAnimationNone,   // available in iOS 3.0
 UITableViewRowAnimationMiddle,   // available in iOS 3.2. attempts to keep cell centered in the space it will/did occupy
 UITableViewRowAnimationAutomatic = 100 // available in iOS 5.0. chooses an appropriate animation style for you
};

相關(guān)文章

  • iOS實(shí)現(xiàn)步驟進(jìn)度條功能實(shí)例代碼

    iOS實(shí)現(xiàn)步驟進(jìn)度條功能實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)步驟進(jìn)度條功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • 解決Alamofire庫在iOS7下設(shè)置Head無效的問題

    解決Alamofire庫在iOS7下設(shè)置Head無效的問題

    本文主要介紹Alamofire庫在iOS下設(shè)置Head,這里通過代碼實(shí)例解決不同版本的IOS系統(tǒng)出現(xiàn)的問題,有需要的小伙伴可以參考下
    2016-07-07
  • IOS開發(fā)中加載大量網(wǎng)絡(luò)圖片優(yōu)化方法

    IOS開發(fā)中加載大量網(wǎng)絡(luò)圖片優(yōu)化方法

    這篇文章主要介紹了IOS開發(fā)中加載大量網(wǎng)絡(luò)圖片如何優(yōu)化的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • iOS中如何引用另一個(gè)工程的方法教程

    iOS中如何引用另一個(gè)工程的方法教程

    在iOS開發(fā)中,引用另一個(gè)工程是大家可能會(huì)遇到的一個(gè)問題,所以這篇文章主要給大家介紹了關(guān)于iOS中如何引用另一個(gè)工程的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • iOS使用 CABasicAnimation 實(shí)現(xiàn)簡(jiǎn)單的跑馬燈(無cpu暴漲)

    iOS使用 CABasicAnimation 實(shí)現(xiàn)簡(jiǎn)單的跑馬燈(無cpu暴漲)

    本篇文章主要介紹了iOS使用 CABasicAnimation 實(shí)現(xiàn)簡(jiǎn)單的跑馬燈(無cpu暴漲),具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-01-01
  • 詳解iOS Method Swizzling使用陷阱

    詳解iOS Method Swizzling使用陷阱

    這篇文章主要介紹了詳解iOS Method Swizzling使用陷阱,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • iOS中設(shè)置圓角的幾種方法示例

    iOS中設(shè)置圓角的幾種方法示例

    這篇文章主要介紹了iOS中設(shè)置圓角的三種方法,其中包括使用layer屬性、使用繪圖設(shè)置圓角以及通過另一張mask圖創(chuàng)建新圖,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-03-03
  • iOS開發(fā)第三方鍵盤處理實(shí)例代碼

    iOS開發(fā)第三方鍵盤處理實(shí)例代碼

    本篇文章主要介紹了iOS開發(fā)第三方鍵盤處理實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • 學(xué)習(xí)iOS開關(guān)按鈕UISwitch控件

    學(xué)習(xí)iOS開關(guān)按鈕UISwitch控件

    這篇文章主要為大家詳細(xì)介紹了iOS開關(guān)按鈕UISwitch控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 深入淺析IOS中UIControl

    深入淺析IOS中UIControl

    UIControl,相信大家對(duì)其并不陌生吧,比如平常最常用的UIButton就是繼承自UIControl的。下面通過本篇文章給大家介紹ios中UIControl,感興趣的朋友一起學(xué)習(xí)吧
    2015-10-10

最新評(píng)論