UI:UITableView表视图
表視圖 UITableView,iOS中最重要的視圖,隨處可?見(jiàn)。 表視圖通常?用來(lái)管理?一組具有相同數(shù)據(jù)結(jié)構(gòu)的數(shù)據(jù)。?
UITableView繼承?自UIScrollView,所以可以滾動(dòng),表視圖的每?一條數(shù)據(jù)都是顯?示在UITableViewCell對(duì)象中,表視圖可以分區(qū)顯?示數(shù)據(jù),每個(gè)分區(qū)稱為?一個(gè)section,每?一?行稱為 row,編號(hào)都是從0開(kāi)始
表視圖的創(chuàng)建(重要屬性)
style樣式:plain、group 分隔線樣式:separatorStyle 分隔線顏色:separateColor 行高: rowheight 去掉點(diǎn)擊后產(chǎn)生的灰色背景色:cell.selectionStyle=UITableViewCellSelectionStyleNone;?
?DataSource數(shù)據(jù)源
我們需要給tableView指定?一個(gè)數(shù)據(jù)源,它負(fù)責(zé)給tableView提供數(shù)據(jù) 需要實(shí)現(xiàn)協(xié)議中兩個(gè)必須實(shí)現(xiàn)的?方法?
- (NSInteger)tableView:(UITableView *)tableView
我們需要給tableView指定?一個(gè)數(shù)據(jù)源,它負(fù)責(zé)給tableView提供數(shù)據(jù)
需要實(shí)現(xiàn)協(xié)議中兩個(gè)必須實(shí)現(xiàn)的?方法:
-numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
UITableView中每?一個(gè)單元格,被稱為?一個(gè)cell
(UITableViewCell)。 系統(tǒng)預(yù)置了4種(枚舉)樣式的cell。 不同樣式的cell包含的控件有細(xì)微差別。
設(shè)置圖片:imageView 設(shè)置文本:textLable 指定選中效果:selectionStyle 指定輔助效果樣式:accessoryType表視圖的重用機(jī)制
UITableView靠mutableSet來(lái)實(shí)現(xiàn)重?用功能
出屏幕的cell會(huì)被添加到mutableSet中,進(jìn)?入屏幕的cell,先從set中 獲取,如果獲取不到,才創(chuàng)建?一個(gè)cell。在cell顯?示之前,給cell賦上相應(yīng)的內(nèi)容。
cell的reuseIdentifier是重?用的關(guān)鍵。?
表視圖的配置
NSIndesPath: row 、section
+(NSIndesPath *)indexPathForRow:(NSUInteger)row inSection:(NSUInteger)section;
多個(gè)分區(qū)
tableView默認(rèn)是?一個(gè)分區(qū),可以設(shè)置多個(gè)分區(qū) tableView的plain、group樣式?jīng)Q定分區(qū)的樣式不同
每個(gè)分區(qū)可以設(shè)置區(qū)頭區(qū)尾
tableView默認(rèn)是?一個(gè)分區(qū),可以設(shè)置多個(gè)分區(qū)
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView; //分區(qū)數(shù)
- (NSString *)tableView:(UITableView *)tableView
tableView的plain、group樣式?jīng)Q定分區(qū)的樣式不同 titleForHeaderInSection:(NSInteger)section; //分區(qū)頭標(biāo)題- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; //右側(cè)豎排索引?
自定義頭尾
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;// 設(shè)置tableView的分割線的樣式 // self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;//不是很明顯的線self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//比較明顯的線條 // self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//沒(méi)有分割線//設(shè)置分割線的背景顏色self.tableView.separatorColor = [UIColor redColor];//設(shè)置分割線的左右距離self.tableView.separatorInset = UIEdgeInsetsMake(150, 10, 10, 20);//分割線的寬度 ???/**下面的兩個(gè):一個(gè)表頭,一個(gè)表尾都是與分組沒(méi)有任何關(guān)系的,只與tableView 的頂部與下部有關(guān)系*///設(shè)置一下表頭的視圖,一般用來(lái)做一個(gè)輪播圖片,這個(gè)圖片要在表頭的上面UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 120)];view.backgroundColor = [UIColor blueColor];self.tableView.tableHeaderView = view;//設(shè)置一下表尾的視圖,一般用于上拉刷新,這個(gè)要在表尾標(biāo)題的下面UIView * view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 120)];view2.backgroundColor = [UIColor redColor];self.tableView.tableFooterView = view2;}//設(shè)置表的表頭的標(biāo)題 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{return @"我是表頭標(biāo)題"; } //設(shè)置表的表尾巴的標(biāo)題 -(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右邊的對(duì)號(hào),用于提示用戶完成一定的操作 // cell.accessoryType = UITableViewCellAccessoryCheckmark;//添加 cell 右邊的 按鈕 + 箭頭 // cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//添加cell 一個(gè) switch 控件 // UISwitch * swit = [[UISwitch alloc] init]; // cell.accessoryView = swit; // [swit addTarget:self action:@selector(valuechange:) forControlEvents:UIControlEventValueChanged];/*//設(shè)置cell 的背景顏色UIImage * image = [UIImage imageNamed:@"img_01.png"];UIImage * image2 = [UIImage imageNamed:@"img_02.png"];cell.backgroundView = [[UIImageView alloc] initWithImage:image];//沒(méi)有選中//選中cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:image2];*/cell.textLabel.text = @"你舅舅家";return cell; } -(void)valuechange:(UISwitch *)swit{NSLog(@"選擇改變了"); }//專門為accessoryType服務(wù),對(duì)自定義控件不響應(yīng) -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{NSLog(@"我是cell 右邊的按鈕"); }//取消選中行的事件 -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{NSLog(@"取消點(diǎn)擊事件"); } //點(diǎn)中某一cell 的事件 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{NSLog(@"確實(shí)選中了某一行"); }-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return 12; }@end tableView 應(yīng)用demo轉(zhuǎn)載于:https://www.cnblogs.com/benpaobadaniu/p/4796296.html
總結(jié)
以上是生活随笔為你收集整理的UI:UITableView表视图的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java Native Interfac
- 下一篇: 并发处理的5中模式