你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios表格的操作

ios表格的操作

編輯:IOS開發綜合
一:1、首先在RootViewController類中需要遵從三個協議:;然後定義兩個對象:@property (retain,nonatomic)UITableView *mTableView;
@property (retain,nonatomic)NSArray *Arr;
2、其中對數組初始化,為方便利用系統封裝好的一個:self.Arr=[UIFont familyNames];
對mTableView初始化:self.mTableView=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
//設置頁眉高度
self.mTableView.sectionHeaderHeight=40;
//設置委托對象
self.mTableView.dataSource=self;
self.mTableView.delegate=self;
3、UITableViewDataSource協議中兩個必須實現的方法:
//每個分段中動行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return [self.Arr count];
}

//每行動繪制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *identifer=@"identifer";
UITableViewCell *pCell=[tableView dequeueReusableCellWithIdentifier:identifer];

if (nil==pCell) {

pCell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];
}
//獲取當前行
NSUInteger cellRow=[indexPath row];
//根據行數找到數組中對應下標動數據
NSString *pTempStr=[self.Arr objectAtIndex:cellRow];
//設置文本內容
pCell.textLabel.text=pTempStr;
//設置文本字體
pCell.textLabel.font=[UIFont fontWithName:pTempStr size:18];

pCell.detailTextLabel.text=@"detailText";
pCell.imageView.image=[UIImage imageNamed:@"Default-568h@2x"];
pCell.accessoryType=UITableViewCellAccessoryCheckmark;
return pCell;
}


4、UITableViewDelegate協議中幾個對表格某些屬性加以描述的方法:
//選中某行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

NSUInteger row=[indexPath row];
NSString *pStr=[NSString stringWithFormat:@"你選中了第%d行",row];
//模態視圖
UIActionSheet *pActionSheet=[[UIActionSheet alloc]initWithTitle:@"ActionSheet"delegate:self cancelButtonTitle:@"確認" destructiveButtonTitle:pStr otherButtonTitles:nil, nil];
[pActionSheet showInView:self.view];
//選中行逐漸淡出
[self.mTableView deselectRowAtIndexPath:indexPath animated:YES];
}
//調整行高方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 88;
}
//調整頁眉高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

return 40;

}


二、對表格內容的編輯以及在表格視圖中實現搜索功能:

1、對表格內容進行編輯:除了之前所學過的創建表格視圖的內容,還增加了以下內容及代碼:
在昨天創建表格視圖的基礎上,需要新建一個類MyCell,將類MyCell導入到類ViewController的.m文件裡
在類MyCell中建兩個Label對象和一個Button(可以由自己決定建多少對象),他們的初始化以及加載到視圖過程就省略不提了,重點是將MyCell類導入到類ViewController中所做的與昨天的改變之處:
其他基本不變,主要改變的是如下:
//繪制行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifer=@"identifer";
MYCell *pCell=(MYCell *)[tableView dequeueReusableCellWithIdentifier:identifer];
if (nil==pCell) {
pCell=[[MYCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];
}
pCell.NameLable.text=@"name";
pCell.ColorLable.text=@"color";
return pCell;
}
注意其中有一個轉換,注意不要忘。

2、在表格中實現搜索功能:
注意此時ViewController遵從的是兩個協議;定義四個對象:
UITableView *mTableView;UISearchBar *mSeachBar;
NSMutableArray *Arr1; NSMutableArray *Arr2;
其中表視圖初始化步驟省略,以下所增加代碼:
//searchbar初始化
self.mSeachBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.mTableView.frame.size.width, 30)];
self.mSeachBar.delegate=self;
//設置當前表格的頭視圖
self.mTableView.tableHeaderView=self.mSeachBar;

[self.view addSubview:self.mTableView];
//初始化兩個可變數組
self.Arr1=[[NSMutableArray alloc]initWithCapacity:60];
self.Arr2=[[NSMutableArray alloc]initWithCapacity:60];
for (int i=0; i<60; i++) {
NSString *pTempStr=[NSString stringWithFormat:@"%d",i];
[self.Arr1 addObject:pTempStr];
[self.Arr2 addObject:pTempStr];
}
行數返回值變為:return [self.Arr2 count];
繪制每行中其中一些代碼:
//給每行加載標題(通過數組2的內容進行添加)
NSInteger row=[indexPath row];
pCell.textLabel.text=[self.Arr2 objectAtIndex:row];


//實現搜索功能的方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{

[self.Arr2 removeAllObjects];
for (NSString *str in self.Arr1){
if ([str hasPrefix:self.mSeachBar .text]) {
[self.Arr2 addObject:str];
}
}
[self.mSeachBar resignFirstResponder];
[self.mTableView reloadData];
}


三、表格的添加和刪除操作以及對表格實現分組操作

1、對表格實現添加和刪除操作功能:
與之前我們所學表格內容主要不同點是:在ViewController類中定義了一個可變數組,以及在視圖中加了一個導航,導航右上上方有一按鈕。

//初始化導航按鈕
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

if (self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.navigationItem.title=@"首頁";
UIBarButtonItem *pBtnItem=[[UIBarButtonItem alloc]initWithTitle:@"BegEdit" style:UIBarButtonItemStylePlain target:self action:@selector(tableViewEdit:)];

self.navigationItem.rightBarButtonItem=pBtnItem;

}

return self;
}

//設置導航按鈕標題:
- (void)tableViewEdit:(id)sender{

[sender setTitle:[self.mTableView isEditing]?@"BegEdit":@"EditEdit"];

[self.mTableView setEditing:![self.mTableView isEditing]];
}
//表格是否可編輯:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

if ([indexPath row]==0) {

return NO;
}
return YES;
}
//每行編輯方式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

if ([indexPath row] % 2) {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleInsert;
}

//刪除和增加方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

if (editingStyle==UITableViewCellEditingStyleDelete) {
//在數組中移出對象
[self.mArr removeObjectAtIndex:[indexPath row]];
[self.mTableView beginUpdates];
[self.mTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight ];
[self.mTableView endUpdates];
}else if(editingStyle==UITableViewCellEditingStyleInsert){
[self.mArr insertObject:@"newCell" atIndex:[indexPath row]];
[self.mTableView beginUpdates];
[self.mTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom ];
[self.mTableView endUpdates];
}
}


//是否可以移動
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

if ([indexPath row]==2) {
return NO;
}
return YES;
}
//移動方法:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//獲取移動前的行
NSUInteger fromRow=[sourceIndexPath row];
//將要移動到的行
NSUInteger toRow=[destinationIndexPath row];
//獲取數組中移動前下標對應的對象
id obj=[self.mArr objectAtIndex:fromRow];
//從數組中刪除
[self.mArr removeObjectAtIndex:fromRow];
//在新的位置加入
[self.mArr insertObject:obj atIndex:toRow];
}


注意表格行數返回的是:return [self.mArr count];
每行表格標題設為:NSUInteger row=[indexPath row];
pCell.textLabel.text=[self.mArr objectAtIndex:row];


2、對表格實現分組:
首先建一個表格視圖,注意所建的表格視圖風格是:UITableViewStyleGrouped;
然後創建一個plist文件,創建方法省略(在resource裡面選中property list,然後創建),在plist文件寫入自己想在分組表格中顯示的內容。

獲取plist文件路徑,並將它賦值給字典,然後將字典中的內容分配給數組,實現代碼如下:
NSString *path=[[NSBundle mainBundle]pathForResource:@"Data" ofType:@"plist"];
self.mDic=[NSDictionary dictionaryWithContentsOfFile:path];
self.mArr=[[self.mDic allKeys]sortedArrayUsingSelector:@selector(compare:)];
此時創建的表格視圖每組中行數返回值為:return [[self.mDic objectForKey:[self.mArr objectAtIndex:section]]count];
繪制每行時每行的標題:pCell.textLabel.text=[[self.mDic objectForKey:[self.mArr objectAtIndex:section]]objectAtIndex:row];

//每組的標簽:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

return [self.mArr objectAtIndex:section];
}

//右側索引欄的添加方法:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

return self.mArr;
}

而組數返回值為:return [self.mArr count];


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved