国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

詳解iOS App中UITableView的創建與內容刷新

2020-07-26 03:21:54
字體:
來源:轉載
供稿:網友

UITableView幾乎是iOS開發中用處最廣的一個控件,當然也是要記相當多東西的一個控件。

創建
首先創建一個新的項目,并添加一個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的數據源UITableViewDataSource。
TableView的委托UITableViewDelegate。
如果當前類是繼承自UIViewController,需要添加上面的代碼,如果直接繼承自UITableViewController則不需要添加
然后打MainViewController.m文件,初始化UItableView并顯示在當前窗口

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

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

201642491911192.png (329×491)

第二種:分組UITableViewStyleGrouped

201642491955065.png (330×493)

創建并設置每行顯示的內容

- (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 = @"詳細信息";  return cell; } 

UITableViewCell的樣式也是可以進行設置的,如果不能滿足項目的需要,可以自己定義UITableViewCell的樣式
UITableViewCellStyleDefault

201642492019623.png (330×492)

UITableViewCellStyleSubtitle

201642492040034.png (331×491)

UITableViewCellStyleValue1

201642492059266.png (325×492)

UITableViewCellStyleValue2

201642492116014.png (328×489)

分組的TableView還可以進行內容的分段,是通過下面的方法實現,返回的數字1代表分為1段

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

設置內容縮進

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

201642492158931.png (324×490)

設置cell的行高

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

設置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)

當選擇指定的cell時,彈出UIAlertView顯示選擇的內容

- (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)

滑動選擇的行后刪除

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

201642492330091.png (321×71)

UITableView的刷新:

[self.tableView reloadData];

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

刷新局部cell:

復制代碼 代碼如下:

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

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

局部刷新section:

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

上面這段代碼是刷新第0個section。

刷新動畫:

刷新UITableView還有幾個動畫:

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};

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汨罗市| 社旗县| 察雅县| 齐齐哈尔市| 北京市| 淳安县| 高唐县| 崇明县| 平遥县| 罗平县| 嘉鱼县| 额敏县| 师宗县| 航空| 万源市| 阳江市| 广灵县| 新晃| 沂源县| 冷水江市| 调兵山市| 桃园市| 白沙| 佛坪县| 延安市| 昌宁县| 临安市| 长阳| 玉龙| 龙泉市| 毕节市| 武穴市| 惠水县| 阳信县| 广昌县| 上高县| 林西县| 册亨县| 嘉义市| 上杭县| 水城县|