講解iOS開發(fā)中UITableView列表設(shè)計(jì)的基本要點(diǎn)
一、UITableView簡(jiǎn)單介紹
1.tableView是一個(gè)用戶可以滾動(dòng)的多行單列列表,在表視圖中,每一行都是一個(gè)UITableViewCell對(duì)象,表視圖有兩種風(fēng)格可選
typedef NS_ENUM(NSInteger, UITableViewStyle) {
UITableViewStylePlain, // regular table view
UITableViewStyleGrouped // preferences style table view
};
2.表視圖還可為其添加索引值,比如通訊錄中右側(cè)索引列表,每一個(gè)索引項(xiàng)對(duì)應(yīng)其節(jié)頭標(biāo)題
這兩種形式的列表下面還會(huì)介紹到。
3.最簡(jiǎn)單的一種表視圖是一個(gè)選擇列表,可以限制選擇一列或多列,如上圖右邊。
4.頁眉和頁腳,可以根據(jù)自己的需要,對(duì)tableView設(shè)置頁眉和頁腳的內(nèi)容
二、UITableViewCell
1. UITableViewCell是表視圖的單元格,系統(tǒng)會(huì)緩存可見的行。通過完成UITableViewDataSource協(xié)議中必須完成的代理方法CellForRowAtIndexPath方法來填充表視圖上單元格數(shù)據(jù)。
2. UITableViewCell有四種樣式可選
UITableViewCellStyleDefault, // 簡(jiǎn)單包含一個(gè)可選的imageView和一個(gè)label顯示文本
UITableViewCellStyleValue1, // 包含可選的imageView,一個(gè)textLabel和一個(gè)detailLabel,其中detailLabel位置在最左,右對(duì)齊,文本顏色為藍(lán)色
UITableViewCellStyleValue2, //包含一個(gè)textLabel和一個(gè)detailLabel,textLabel默認(rèn)為藍(lán)色文本,右對(duì)齊,detailLabel的位置緊挨著textLabel右邊,默認(rèn)文本左對(duì)齊,顏色為黑色
UITableViewCellStyleSubtitle // 包含可選的imageView,一個(gè)textLabel,一個(gè)detailLabel,其中detailLabel在textLabel下方,字體較小,默認(rèn)顏色為黑色,左對(duì)齊
三、創(chuàng)建簡(jiǎn)單TableView
1. 先給出效果圖
2. 創(chuàng)建方式及代碼(本文只講述代碼創(chuàng)建)
a) 創(chuàng)建一個(gè)Single View Application,命名為"tableView"
b) 新建一個(gè)繼承自UITableView的類,關(guān)于tableView的實(shí)現(xiàn)將全部寫在這個(gè)類中(當(dāng)然也可直接在對(duì) 應(yīng)所需要用得ViewController中創(chuàng)建,分離出來的好處是可以在將tableView的方法單獨(dú)放在一個(gè)類中,當(dāng)ViewController的代碼量比較大或者這個(gè)table需要在多個(gè)地方使用時(shí)推薦使用),命名為general_table_view.
c) 代碼
①在general_table_view.h文件中,添加幾個(gè)屬性
@interface general_table_view : UITableView
// tableView的坐標(biāo)
@property (nonatomic, assign) CGRect tableViewFrame;
// 存放Cell上各行textLabel值
@property (nonatomic, copy)NSMutableArray * textLabel_MArray;
// 存放Cell上各行imageView上圖片
@property (nonatomic, copy)NSMutableArray * images_MArray;
// 存放Cell上各行detailLabel值
@property (nonatomic, copy)NSMutableArray * subtitle_MArray;
@end
②在general_table_view.m的interface中聲明代理
@interface general_table_view ()<UITableViewDataSource,UITableViewDelegate>
@end
③在.m中的initWithFrame方法內(nèi)部設(shè)置table的代理
// Initialization code
self.delegate = self;
self.dataSource = self;
以及添加tableViewFrame的set方法
-(void)setTableViewFrame:(CGRect)tableViewFrame
{
self.frame = tableViewFrame;// 設(shè)置tableView的frame為所傳值
}
④接下來實(shí)現(xiàn)tableView的dataSource和delegate方法
必須實(shí)現(xiàn)的方法有兩個(gè)
// tableView每個(gè)分區(qū)的行數(shù),可以為各個(gè)分區(qū)設(shè)置不同的行數(shù),根據(jù)section的值判斷即可
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_textLabel_MArray count];
}
// 實(shí)現(xiàn)每一行Cell的內(nèi)容,tableView重用機(jī)制
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 為其定義一個(gè)標(biāo)識(shí)符,在重用機(jī)制中,標(biāo)識(shí)符非常重要,這是系統(tǒng)用來匹配table各行cell的判斷標(biāo)準(zhǔn),在以后的學(xué)習(xí)中會(huì)體會(huì)到
static NSString *cellIdentifier = @"cellIdentifier";
// 從緩存隊(duì)列中取出復(fù)用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// 如果隊(duì)列中cell為空,即無復(fù)用的cell,則對(duì)其進(jìn)行初始化
if (cell==nil) {
// 初始化
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// 定義其輔助樣式
cell.accessoryType = UITableViewCellAccessoryNone;
}
// 設(shè)置cell上文本內(nèi)容
cell.textLabel.text = [_textLabel_MArray objectAtIndex:indexPath.row];
return cell;
}
⑤還有其他輔助方法,根據(jù)需要添加
// tableView分區(qū)數(shù)量,默認(rèn)為1,可為其設(shè)置為多個(gè)分區(qū)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// tableView頁眉的值,同理,可為不同的分區(qū)設(shè)置不同的頁眉,也可不寫此方法
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"頁眉";
}
// 頁腳
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"頁腳";
}
⑥在所需要添加的ViewController中添加tableView,在ViewController.m方法中
#import "general_table_view.h"
@interface ViewController ()
{
general_table_view *table;// 聲明table
}
@end
并在ViewDidLoad方法中對(duì)其進(jìn)行初始化
// 初始化
table = [[general_table_view alloc] initWithFrame:CGRectMake(0, 20, 320, self.view.frame.size.height-20) style:UITableViewStylePlain];
// 設(shè)置數(shù)據(jù)源
table.textLabel_MArray = [[NSMutableArray alloc] initWithObjects:@"南京市",@"南通市",@"淮安市",@"鎮(zhèn)江市",@"揚(yáng)州市",@"常州市", nil];
[self.view addSubview:table];// 添加到當(dāng)前View
⑦運(yùn)行即可得到圖5的效果,將初始化時(shí)的style改為UITableViewStyleGrouped即可得到圖6的效果
// 初始化
table = [[general_table_view alloc] initWithFrame:CGRectMake(0, 20, 320, self.view.frame.size.height-20) style:UITableViewStyleGrouped];
四、為每一行添加圖片
在ViewController.m的ViewDidLoad方法中設(shè)置數(shù)據(jù)源時(shí),在addSubview之前,初始化一個(gè)存放圖片的數(shù)組,這里我添加的是同一張圖片,如果想為每一行設(shè)置不同的圖片,添加不同的圖片到數(shù)組中即可
NSMutableArray *images = [NSMutableArray array];
for(NSInteger index = 0;index<[table.textLabel_MArray count];index++){
UIImage *image = [UIImage imageNamed:@"2"];
[images addObject:image];
}
table.images_MArray = [[NSMutableArray alloc] initWithArray:images];
在CellForRowAtIndexPath方法中設(shè)置textLabel值部分添加
// 設(shè)置cell上文本內(nèi)容
cell.textLabel.text = [_textLabel_MArray objectAtIndex:indexPath.row];
// 設(shè)置每一行的圖片
cell.imageView.image = [_images_MArray objectAtIndex:indexPath.row];
五、列表的其他樣式
在CellForRowAtIndexPath方法中,初始化Cell時(shí)改變cell的style和accessoryType,style,style默認(rèn)有四種可選。
在ViewController的ViewDidLoad方法中添加圖片的for循環(huán)中為數(shù)組添加值
NSMutableArray *subtitle= [NSMutableArray array];
for(NSInteger index = 0;index<[table.textLabel_MArray count];index++){
UIImage *image = [UIImage imageNamed:@"2"];
NSString *detail = [NSString stringWithFormat:@"detail text %d",index+1];
[images addObject:image];
[subtitle addObject:detail];
}
table.subtitle_MArray = [[NSMutableArray alloc] initWithArray:subtitle];
并在CellForRowAtIndexPath方法初始化時(shí)將
UITableViewCellStyleDefault改變成其他三種樣式,并添加代碼
// 設(shè)置小標(biāo)題
cell.detailTextLabel.text = [_subtitle_MArray objectAtIndex:indexPath.row];
效果圖如下:
六、列表中行的操作
1.選中行
實(shí)現(xiàn)代理方法
// 選中行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"您點(diǎn)擊了第%d分區(qū)第%d行",indexPath.section, indexPath.row);
// 取消選中狀態(tài)
// [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
2.刪除行
要對(duì)行進(jìn)行操作,首先要實(shí)現(xiàn)代理方法
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
先講述單獨(dú)刪除一行數(shù)據(jù),即左滑出現(xiàn)刪除按鈕,并刪除行的操作,后文會(huì)介紹多選批量刪除
可重置刪除按鈕的標(biāo)題,默認(rèn)為"delete"
// 設(shè)置刪除按鈕標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"刪除";
}
點(diǎn)擊刪除后
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 從數(shù)據(jù)源中刪除
[self.dataArray removeObjectAtIndex:indexPath.row];
// 從列表中刪除
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
3.插入行
①這時(shí)我將插入行和刪除行都以一個(gè)按鈕動(dòng)作來觸發(fā),點(diǎn)擊后tableView進(jìn)入編輯模式,先上效果圖
②在ViewDidLoad中添加代碼,其中self.addButton和self.deleteBarButtonItem均在storyBoard中創(chuàng)建,下文中的按鈕也是這種情況
NSArray *leftBarButtons = [NSArray arrayWithObjects:self.addButton,self.deleteBarButtonItem, nil];
self.navigationItem.leftBarButtonItems = leftBarButtons;//設(shè)置導(dǎo)航欄左邊按鈕為添加和刪除按鈕
③在@interface中聲明一個(gè)變量
UITableViewCellEditingStyle selectEditingStyle;
④兩個(gè)按鈕的點(diǎn)擊事件
// 更新導(dǎo)航欄按鈕
-(void) updateBarButtons
{
if (self.tableView.editing==YES) {
self.navigationItem.rightBarButtonItem = self.doneBarButtonItem;
}
}
// 點(diǎn)擊添加按鈕
- (IBAction)addButtonClicked:(id)sender {
selectEditingStyle = UITableViewCellEditingStyleInsert;
[self.tableView setEditing:YES animated:YES];
[self updateBarButtons];
}
// 點(diǎn)擊刪除按鈕
- (IBAction)deleteButtonClicked:(id)sender {
selectEditingStyle = UITableViewCellEditingStyleDelete;
[self.tableView setEditing:YES animated:YES];
[self updateBarButtons];
}
⑤實(shí)現(xiàn)相應(yīng)的代理方法
// 是否可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// 編輯模式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return selectEditingStyle;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 刪除模式
if (editingStyle==UITableViewCellEditingStyleDelete) {
// 從數(shù)據(jù)源中刪除
[self.dataArray removeObjectAtIndex:indexPath.row];
// 刪除行
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
// 添加模式
else if(editingStyle == UITableViewCellEditingStyleInsert){
// 從數(shù)據(jù)源中添加
[self.dataArray insertObject:@"new iPhone" atIndex:indexPath.row];
// 添加行
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic ];
}
}
// 點(diǎn)擊完成按鈕
- (IBAction)doneButtonClicked:(id)sender {
[self.tableView setEditing:NO animated:YES];
[self updateBarButtons];
}
4.移動(dòng)行
①效果圖
②在tableView進(jìn)入編輯模式時(shí),可以對(duì)行進(jìn)行移動(dòng)操作,通過方法
// 是否支持移動(dòng)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
③設(shè)置行可移動(dòng),并完成移動(dòng)行方法,改變數(shù)據(jù)源
// 移動(dòng)行操作-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{// 這里其實(shí)就是數(shù)組中兩個(gè)變量交換位置的過程 id object = [self.dataArray objectAtIndex:fromIndexPath.row];
[self.dataArray removeObjectAtIndex:fromIndexPath.row];
[self.dataArray insertObject:object atIndex:toIndexPath.row];
}
5、批量刪除行
①即完成可以選擇多個(gè)行之后批量刪除,如圖
②在ViewDidLoad中添加代碼
self.navigationItem.rightBarButtonItem = self.editBarButtonItem;// 在右導(dǎo)航欄中添加編輯按鈕
③現(xiàn)在需要達(dá)到,點(diǎn)擊編輯按鈕在右上角出現(xiàn)取消按鈕,左上角出現(xiàn)刪除按鈕。并在選擇時(shí),能出現(xiàn)刪除行的數(shù)量,修改updateBarButtons方法,并添加一個(gè)方法來根據(jù)條件修改刪除按鈕的標(biāo)題
// 更新導(dǎo)航欄按鈕
-(void) updateBarButtons
{
// 如果是允許多選的狀態(tài),即進(jìn)入批量刪除模式
if (self.tableView.allowsSelectionDuringEditing == YES) {
//更新刪除按鈕
[self updateDeleteButtonTitle];
// 導(dǎo)航欄左邊按鈕設(shè)置為空
self.navigationItem.leftBarButtonItems = nil;
// 將左邊按鈕設(shè)置為'批量刪除'按鈕
self.navigationItem.leftBarButtonItem = self.multiDeleteBarButton;
// 導(dǎo)航欄右鍵設(shè)置為'取消'鍵
self.navigationItem.rightBarButtonItem = self.cancelBarButtonItem;
return;
}
if (self.tableView.editing==YES) {// 如果是編輯狀態(tài),且不屬于批量刪除狀態(tài)
// 導(dǎo)航欄右鍵設(shè)置為'取消'鍵
self.navigationItem.rightBarButtonItem = self.doneBarButtonItem;
}
else {// 如果不是編輯狀態(tài),將導(dǎo)航欄設(shè)置為初始狀態(tài)的樣式,即左欄為'添加','刪除'按鈕,右欄為'編輯'按鈕
NSArray *leftBarButtons = [NSArray arrayWithObjects:self.addButton,self.deleteBarButtonItem, nil];
self.navigationItem.leftBarButtonItems = leftBarButtons;
self.navigationItem.rightBarButtonItem = self.editBarButtonItem;
}
}
// 更新刪除按鈕的標(biāo)題
-(void)updateDeleteButtonTitle
{
NSArray *selectedRows = [self.tableView indexPathsForSelectedRows];//得到選中行
BOOL allItemsAreSelected = selectedRows.count == self.dataArray.count;// 是否全選
BOOL noItemsAreSelected = selectedRows.count == 0;// 選中行數(shù)是否為零
if (allItemsAreSelected || noItemsAreSelected)
{// 如果是全選或者未選,則刪除鍵為刪除全部
self.multiDeleteBarButton.title = @"刪除全部";
}
else
{// 否則 刪除鍵為刪除(選中行數(shù)量)
self.multiDeleteBarButton.title = [NSString stringWithFormat:@"刪除 (%d)", selectedRows.count];
}
}
④在
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
兩個(gè)方法中調(diào)用updateDeleteButtonTitle方法
⑤點(diǎn)擊編輯按鈕時(shí)
// 編輯按鈕
- (IBAction)editButtonClicked:(id)sender {
self.tableView.allowsMultipleSelectionDuringEditing = YES;// 進(jìn)入可多選刪除狀態(tài)
[self.tableView setEditing:YES animated:YES];// 將table設(shè)置為可編輯
[self updateBarButtons]; //更改導(dǎo)航欄的導(dǎo)航按鈕
}
⑥點(diǎn)擊刪除多個(gè)按鈕時(shí)
- (IBAction)multiDeleteClicked:(id)sender {
// 選中的行
NSArray *selectedRows = [self.tableView indexPathsForSelectedRows];
// 是否刪除特定的行
BOOL deleteSpecificRows = selectedRows.count > 0;
// 刪除特定的行
if (deleteSpecificRows)
{
// 將所選的行的索引值放在一個(gè)集合中進(jìn)行批量刪除
NSMutableIndexSet *indicesOfItemsToDelete = [NSMutableIndexSet new];
for (NSIndexPath *selectionIndex in selectedRows)
{
[indicesOfItemsToDelete addIndex:selectionIndex.row];
}
// 從數(shù)據(jù)源中刪除所選行對(duì)應(yīng)的值
[self.dataArray removeObjectsAtIndexes:indicesOfItemsToDelete];
//刪除所選的行
[self.tableView deleteRowsAtIndexPaths:selectedRows withRowAnimation:UITableViewRowAnimationAutomatic];
}
else
{
// 刪除全部
[self.dataArray removeAllObjects];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
}
// 刪除完成,退出編輯狀態(tài),并退出多選狀態(tài),同時(shí)更新導(dǎo)航欄的按鈕
[self.tableView setEditing:NO animated:YES];
self.tableView.allowsMultipleSelectionDuringEditing = NO;
[self updateBarButtons];
}
- iOS實(shí)現(xiàn)列表與網(wǎng)格兩種視圖的相互切換
- iOS多級(jí)列表實(shí)現(xiàn)代碼
- IOS展開三級(jí)列表效果示例
- IOS實(shí)現(xiàn)展開二級(jí)列表效果
- IOS實(shí)現(xiàn)簡(jiǎn)易版的QQ下拉列表
- iOS功能實(shí)現(xiàn)之列表的橫向刷新加載
- 使用UItableview在iOS應(yīng)用開發(fā)中實(shí)現(xiàn)好友列表功能
- Android實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)下拉框 下拉列表spinner的實(shí)例代碼
- jquery select(列表)的操作(取值/賦值)
- ios基于UITableViewController實(shí)現(xiàn)列表
相關(guān)文章
詳解iOS App中UITableView的創(chuàng)建與內(nèi)容刷新
這篇文章主要介紹了iOS App中UITableView的創(chuàng)建與內(nèi)容刷新,講解了UITableView一些基本的樣式與cell的設(shè)置及刷新,需要的朋友可以參考下2016-04-04關(guān)于iOS 11下app圖標(biāo)變空白問題的解決方法
升級(jí)到iOS11系統(tǒng)下自己的項(xiàng)目桌面app圖標(biāo)不見了,通過查找相關(guān)的資料終于找到了解決方法,下面這篇文章主要給大家介紹了關(guān)于iOS 11下app圖標(biāo)變空白問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12IOS動(dòng)畫效果源代碼整理(粒子、雪花、火焰、河流、蒸汽)
本篇文章給大家整理的IOS的關(guān)于動(dòng)畫的效果代碼整理,很多效果非常的好看,有興趣的學(xué)下。2018-01-01IOS ObjectC與javascript交互詳解及實(shí)現(xiàn)代碼
這篇文章主要介紹了IOS OC與js交互詳解及實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03