天天看點

UITableView中行的操作

這篇文章主要講的表格的操作包括:标記行、移動行、删除行、插入行。

這次就不從頭建立工程了,在http://www.oschina.net/code/snippet_164134_9876下載下傳工程。這個工程就是最簡單的産生一個表格并向其中寫入資料。用Xcode 4.2打開它,在這個工程基礎上實作以上操作。

1、标記行

這裡講的标記行指的是單擊此行,可以實作在此行右邊出現一個勾,如下圖所示:

UITableView中行的操作

為了實作标記功能,在ViewController.m中@end之前添加代碼:

#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *oneCell = [tableView cellForRowAtIndexPath: indexPath];
    if (oneCell.accessoryType == UITableViewCellAccessoryNone) {
        oneCell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else 
        oneCell.accessoryType = UITableViewCellAccessoryNone;
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
}      

該代碼實作:單擊某行時,若此行未被标記,則标記此行;若此行已經被标記,則取消标記。

運作效果如上圖。

上面的代碼實際上就是修改某行的accessoryType屬性,這個屬性可以設為四個常量:

UITableViewCellAccessoryCheckmark
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryNone      

效果依次如下圖所示:

UITableView中行的操作
UITableView中行的操作

   UITableViewCellAccessoryCheckmark            UITableViewCellAccessoryDetailDisclosureButton

UITableView中行的操作
UITableView中行的操作

UITableViewCellAccessoryDisclosureIndicator                   UITableViewCellAccessoryNone

注意,上面第二張圖檔中的藍色圓圈不僅僅是一個圖示,還是一個控件,點選它可以觸發事件,在上一篇部落格《iOS開發16:使用Navigation Controller切換視圖》使用過。

2、移動行

想要實作移動或者删除行這樣的操作,需要啟動表格的編輯模式。使用的是setEditing:animated:方法。

2.1 打開ViewController.xib,将其中的表格控件映射成Outlet到ViewController.h,名稱為myTableView。

2.2 打開ViewController.m,在viewDidLoad方法最後添加代碼:

//啟動表格的編輯模式
[self.myTableView setEditing:YES animated:YES];      

2.3 在@end之前添加代碼:

//打開編輯模式後,預設情況下每行左邊會出現紅的删除按鈕,這個方法就是關閉這些按鈕的
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
           editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return UITableViewCellEditingStyleNone; 
} 

//這個方法用來告訴表格 這一行是否可以移動
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { 
    return YES; 
}

//這個方法就是執行移動操作的
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)
        sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    NSUInteger fromRow = [sourceIndexPath row]; 
    NSUInteger toRow = [destinationIndexPath row]; 
    
    id object = [list objectAtIndex:fromRow]; 
    [list removeObjectAtIndex:fromRow]; 
    [list insertObject:object atIndex:toRow]; 
}      

editingStyleForRowAtIndexPath這個方法中用到了常量UITableViewCellEditingStyleNone,它表示不可編輯,這裡的編輯指的是删除和插入。表示表格行的編輯模式的常量有:

UITableViewCellEditingStyleDelete
UITableViewCellEditingStyleInsert
UITableViewCellEditingStyleNone      

顧名思義,第一個表示删除,第二個表示插入,第三個表示不可編輯。

若将editingStyleForRowAtIndexPath方法中的UITableViewCellEditingStyleNone依次換成上面三個值,則它們運作的效果依次如下圖所示:

UITableView中行的操作
UITableView中行的操作
UITableView中行的操作

2.4 運作,從下圖可以看到實作了行的移動:

UITableView中行的操作

但是也會發現,現在無法對每行進行标記了。這說明,在編輯模式下,無法選擇行,進而didSelectRowAtIndexPath這個方法不會執行。

3、删除行

從第2步過來,實作删除某行,其實比較簡單了。

3.1将editingStyleForRowAtIndexPath方法中的UITableViewCellEditingStyleNone修改成UITableViewCellEditingStyleDelete。

3.2 在@end之前添加代碼:

//這個方法根據參數editingStyle是UITableViewCellEditingStyleDelete
//還是UITableViewCellEditingStyleDelete執行删除或者插入
- (void)tableView:(UITableView *)tableView commitEditingStyle:
    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.list removeObjectAtIndex:row]; 
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationAutomatic]; 
    }
}      

在這個方法中又出現了一個常量:UITableViewRowAnimationAutomatic,它表示删除時的效果,類似的常量還有:

UITableViewRowAnimationAutomatic
UITableViewRowAnimationTop
UITableViewRowAnimationBottom
UITableViewRowAnimationLeft
UITableViewRowAnimationRight
UITableViewRowAnimationMiddle
UITableViewRowAnimationFade
UITableViewRowAnimationNone      

它們的效果就不一一介紹了,可以在實際使用時試試。

3.3 運作,看看效果:

UITableView中行的操作
UITableView中行的操作
UITableView中行的操作

剛運作時顯示如左邊的圖檔,點選某一行左邊的圓圈圖示,會顯示如中間圖檔所示。然後點選Delegate按鈕,那一行就會被删除掉,如右邊的那張圖檔所示,它顯示的是删除時的效果。

4、插入行

這個與删除行類似。

4.1 首先将editingStyleForRowAtIndexPath方法中的UITableViewCellEditingStyleDelete修改成UITableViewCellEditingStyleInsert。

4.2在3.2添加的方法中添加代碼:

else {
    //我們實作的是在所選行的位置插入一行,是以直接使用了參數indexPath
    NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];
    //同樣,将資料加到list中,用的row
    [self.list insertObject:@"新添加的行" atIndex:row];
    [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
}      

上面的代碼中也可以不用insertRowsAtIndexPaths方法,而直接使用[tableView reloadData];語句,但是這樣就沒有添加的效果了。

4.3 好了,運作一下:

UITableView中行的操作
UITableView中行的操作
UITableView中行的操作

剛運作時如上面左圖所示,單擊了某個加号後,新的一行就從右邊飛進來了,因為在insertRowsAtIndexPaths中用了參數UITableViewRowAnimationRight。

UITableView每個cell之間的預設分割線如何去掉

很簡單,隻需要

tableView.separatorStyle = NO;

UITableView

-、建立 UITableView

 DataTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 420)];

 [DataTable setDelegate:self];

 [DataTable setDataSource:self];

 [self.view addSubview:DataTable];

 [DataTable release];

二、UITableView各Method說明

//Section總數

- (NSArray *)sectionIndexTitlesForTableView:(UITableView*)tableView{

 return TitleData;

}

// Section Titles

//每個section顯示的标題

- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{

 return @"";

}

//指定有多少個分區(Section),預設為1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

 return 4;

}

//指定每個分區中有多少行,預設為1

- (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section{

}

//繪制Cell

-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *SimpleTableIdentifier [email protected]"SimpleTableIdentifier";

   UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:

                            SimpleTableIdentifier];

    if (cell ==nil) {  

       cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault

                                      reuseIdentifier: SimpleTableIdentifier] autorelease];

 }

 cell.imageView.image=image;//未選cell時的圖檔

 cell.imageView.highlightedImage=highlightImage;//選中cell後的圖檔

 cell.text=//.....

 return cell;

}

//行縮進

-(NSInteger)tableView:(UITableView *)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{

 NSUInteger row = [indexPath row];

 return row;

}

//改變行的高度

- (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return40;

}

//定位

[TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 +Chapter * 20)];

//傳回目前所選cell

NSIndexPath *ip = [NSIndexPath indexPathForRow:rowinSection:section];

[TopicsTable selectRowAtIndexPath:ip animated:YESscrollPosition:UITableViewScrollPositionNone];

[tableViewsetSeparatorStyle:UITableViewCellSelectionStyleNone];

//選中Cell響應事件

- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{

 [tableView deselectRowAtIndexPath:indexPathanimated:YES];//選中後的反顯顔色即刻消失

}

//判斷選中的行(阻止選中第一行)

-(NSIndexPath *)tableView:(UITableView *)tableViewwillSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSUIntegerrow = [indexPath row];

    if (row ==0)

       return nil;

    returnindexPath;

}

//劃動cell是否出現del按鈕

- (BOOL)tableView:(UITableView *)tableViewcanEditRowAtIndexPath:(NSIndexPath *)indexPath {

}

//編輯狀态

- (void)tableView:(UITableView *)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

{

[topicsTable setContentSize:CGSizeMake(0,controller.promiseNum *44)];

//右側添加一個索引表

- (NSArray *)sectionIndexTitlesForTableView:(UITableView*)tableView{

}

//傳回Section标題内容

- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{

}

//自定義劃動時del按鈕内容

- (NSString *)tableView:(UITableView *)tableView

titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath

//跳到指的row or section

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0inSection:0] atScrollPosition:UITableViewScrollPositionBottomanimated:NO];

三、在UITableViewCell上建立UILable多行顯示

- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {

    staticNSString *CellIdentifier [email protected]"Cell";   

   UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell ==nil) {

       cell = [[[UITableViewCell alloc] initWithFrame:CGRectZeroreuseIdentifier:CellIdentifier] autorelease];

  UILabel *Datalabel = [[UILabelalloc] initWithFrame:CGRectMake(10, 0, 320, 44)];

  [Datalabel setTag:100];

  Datalabel.autoresizingMask =UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;

  [cell.contentViewaddSubview:Datalabel];

  [Datalabel release];

 } 

 UILabel *Datalabel = (UILabel *)[cell.contentViewviewWithTag:100];

 [Datalabel setFont:[UIFontboldSystemFontOfSize:18]];

 Datalabel.text = [data.DataArrayobjectAtIndex:indexPath.row];

 cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;

    returncell;

}

//選中cell時的顔色

typedef enum {

   UITableViewCellSelectionStyleNone,

   UITableViewCellSelectionStyleBlue,

   UITableViewCellSelectionStyleGray

} UITableViewCellSelectionStyle

//cell右邊按鈕格式

typedef enum {

      UITableViewCellAccessory None,                                    // don't show any accessory view

      UITableViewCellAccessory DisclosureIndicator,      // regular chevron. doesn't track

      UITableViewCellAccessory DetailDisclosureButton, // blue button w/chevron. tracks

      UITableViewCellAccessory Checkmark                            // checkmark. doesn't track

} UITableViewCellAccessory Type

//是否加換行線

typedef enum {

   UITableViewCellSeparatorStyleNone,

   UITableViewCellSeparatorStyleSingleLine

} UITableViewCellSeparatorStyle

//改變換行線顔色

tableView.separatorColor = [UIColor blueColor];