UI:UITableView表视图
表視圖 UITableView,iOS中最重要的視圖,隨處可?見。 表視圖通常?用來管理?一組具有相同數據結構的數據。?
UITableView繼承?自UIScrollView,所以可以滾動,表視圖的每?一條數據都是顯?示在UITableViewCell對象中,表視圖可以分區顯?示數據,每個分區稱為?一個section,每?一?行稱為 row,編號都是從0開始
表視圖的創建(重要屬性)
style樣式:plain、group 分隔線樣式:separatorStyle 分隔線顏色:separateColor 行高: rowheight 去掉點擊后產生的灰色背景色:cell.selectionStyle=UITableViewCellSelectionStyleNone;?
?DataSource數據源
我們需要給tableView指定?一個數據源,它負責給tableView提供數據 需要實現協議中兩個必須實現的?方法?
- (NSInteger)tableView:(UITableView *)tableView
我們需要給tableView指定?一個數據源,它負責給tableView提供數據
需要實現協議中兩個必須實現的?方法:
-numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
UITableView中每?一個單元格,被稱為?一個cell
(UITableViewCell)。 系統預置了4種(枚舉)樣式的cell。 不同樣式的cell包含的控件有細微差別。
設置圖片:imageView 設置文本:textLable 指定選中效果:selectionStyle 指定輔助效果樣式:accessoryType表視圖的重用機制
UITableView靠mutableSet來實現重?用功能
出屏幕的cell會被添加到mutableSet中,進?入屏幕的cell,先從set中 獲取,如果獲取不到,才創建?一個cell。在cell顯?示之前,給cell賦上相應的內容。
cell的reuseIdentifier是重?用的關鍵。?
表視圖的配置
NSIndesPath: row 、section
+(NSIndesPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;
多個分區
tableView默認是?一個分區,可以設置多個分區 tableView的plain、group樣式決定分區的樣式不同
每個分區可以設置區頭區尾
tableView默認是?一個分區,可以設置多個分區
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView; //分區數
- (NSString *)tableView:(UITableView *)tableView
tableView的plain、group樣式決定分區的樣式不同 titleForHeaderInSection:(NSInteger)section; //分區頭標題- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; //右側豎排索引?
自定義頭尾
Delegate- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; View Code?自定義頭尾?單元格的高度與選中
Delegate- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath View Code?單元格的高度與選中?參考1? 參考2? 參考3
代碼: #import "ViewController.h"@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> @property (weak, nonatomic) IBOutlet UITableView *tableView;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.tableView.rowHeight = 160;// 設置tableView的分割線的樣式 // self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;//不是很明顯的線self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//比較明顯的線條 // self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//沒有分割線//設置分割線的背景顏色self.tableView.separatorColor = [UIColor redColor];//設置分割線的左右距離self.tableView.separatorInset = UIEdgeInsetsMake(150, 10, 10, 20);//分割線的寬度 ???/**下面的兩個:一個表頭,一個表尾都是與分組沒有任何關系的,只與tableView 的頂部與下部有關系*///設置一下表頭的視圖,一般用來做一個輪播圖片,這個圖片要在表頭的上面UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 120)];view.backgroundColor = [UIColor blueColor];self.tableView.tableHeaderView = view;//設置一下表尾的視圖,一般用于上拉刷新,這個要在表尾標題的下面UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 120)];view2.backgroundColor = [UIColor redColor];self.tableView.tableFooterView = view2;}//設置表的表頭的標題 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{return @"我是表頭標題"; } //設置表的表尾巴的標題 -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{return @"我是表的表尾巴"; }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return 2; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];//添加的cell右邊的 buttoncell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];//添加cell右邊的 按鈕 // cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//添加 cell右邊的對號,用于提示用戶完成一定的操作 // cell.accessoryType = UITableViewCellAccessoryCheckmark;//添加 cell 右邊的 按鈕 + 箭頭 // cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//添加cell 一個 switch 控件 // UISwitch * swit = [[UISwitch alloc] init]; // cell.accessoryView = swit; // [swit addTarget:self action:@selector(valuechange:) forControlEvents:UIControlEventValueChanged];/*//設置cell 的背景顏色UIImage * image = [UIImage imageNamed:@"img_01.png"];UIImage * image2 = [UIImage imageNamed:@"img_02.png"];cell.backgroundView = [[UIImageView alloc] initWithImage:image];//沒有選中//選中cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:image2];*/cell.textLabel.text = @"你舅舅家";return cell; } -(void)valuechange:(UISwitch *)swit{NSLog(@"選擇改變了"); }//專門為accessoryType服務,對自定義控件不響應 -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{NSLog(@"我是cell 右邊的按鈕"); }//取消選中行的事件 -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{NSLog(@"取消點擊事件"); } //點中某一cell 的事件 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{NSLog(@"確實選中了某一行"); }-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return 12; }@end tableView 應用demo轉載于:https://www.cnblogs.com/benpaobadaniu/p/4796296.html
總結
以上是生活随笔為你收集整理的UI:UITableView表视图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java Native Interfac
- 下一篇: 并发处理的5中模式