IOS基礎:tableview中cell


添加數據源, 由三個函數來回答數據綁定的請求:numberOfSectionsInTableView, numberOfRowsInSection 和 cellForRowAtIndexPath.

用numberOfSectionsInTableView方法來返回table中有幾個組.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

  return 1;

}

用numberOfRowsInSection方法來返回每個組里有幾行

- (NSInteger)tableView:(UITableView *)tableView

 numberOfRowsInSection:(NSInteger)section 

{

  return nRecords;

}

最后用cellForRowAtIndexPath來得到一個包含每一行顯示信息的UITableViewCell對象. UITableViewCell類支持文本和圖像,編輯和刪除確認等功能. 這些信息會保存在表隊列里,用來至此翻頁等功能,但是內存很低的時候會自動釋放,然后再需要的時候重新創建.

- (UITableViewCell *)tableView:(UITableView *)tableView

    cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{

    NSString *CellIdentifier = [ [ NSString alloc ] initWithFormat:

         @"Cell %d", [ indexPath indexAtPosition: 1 ] ];

    

    UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier: CellIdentifier ]; 

    

    if (cell == nil) {

        cell = [ [ [ UITableViewCell alloc ]

            initWithFrame: CGRectZero reuseIdentifier: CellIdentifier ]

        autorelease 

        ]; 

    }

    

    cell.text = CellIdentifier;

    return cell; 

}

NSIndexPath用來保存哪一組的哪一行.

[ indexPath indexAtPosition: 0 ]哪一組

[ indexPath indexAtPosition: 1 ]哪一行

 

7.2 UITableViewCell包含圖像,文本等.

NSString *CellIdentifier = [ [ NSString alloc ] initWithString: @"Frank" ];

UITableViewCell *cell = [ [ [ UITableViewCell alloc ]

        initWithFrame: CGRectZero

        reuseIdentifier: CellIdentifier 

    ] autorelease

];

然后你可以為每一個cell設置不同的風格

(1) 顯示文本: cell.text = @"Frank's Table Cell";

(2) 對齊: cell.textAlignment = UITextAlignmentLeft;

UITextAlignmentLeft 默認是左對齊

UITextAlignmentRight 右對齊

UITextAlignmentCenter 中對齊

(3) 字體和尺寸:

#import <UIKit/UIFont.h>

UIFont *myFont = [ UIFont fontWithName: @"Arial" size: 18.0 ]; 

cell.font = myFont;

//系統字體

UIFont *mySystemFont = [ UIFont systemFontOfSize: 12.0 ];

UIFont *myBoldSystemFont = [ UIFont boldSystemFontOfSize: 12.0 ];

UIFont *myItalicSystemFont = [ UIFont italicSystemFontOfSize: 12.0 ];

(4) 顏色

#import <UIKit/UIColor.h>

//文本顏色

cell.textColor = [ UIColor redColor ];

//當前選擇項的顏色

cell.selectedTextColor = [ UIColor blueColor ];

(5) 圖像

//從你應用程序目錄下的文件創建一個image

cell.image = [ UIImage imageNamed: @"cell.png" ];

//當前選中項的圖形

cell.selectedImage = [ UIImage imageNamed: @"selected_cell.png" ];

可以修改table保准行高來適應你的圖形高度

- (id)init 

{

    self = [ super init ];

    if (self != nil) {

        self.tableView.rowHeight = 65; 

    }

    return self; 

}

你也可以為每一個cell定義不同的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

{

    if ([ indexPath indexAtPosition: 1 ] == 0)

        return 65.0; 

    else

        return 40.0; 

}

(6)選中項的風格

cell.selectionStyle = UITableViewCellSelectionStyleBlue;

UITableViewCellSelectionStyleBlue 默認選中項是藍色

UITableViewCellSelectionStyleGray 灰色

UITableViewCellSelectionStyleNone 沒有變化

 

(7)標簽 (labels)

在偏移量100x0處創建一個尺寸50x50 label:

UILabel *label = [ [ UILabel alloc ] initWithFrame: CGRectMake(100.0, 0.0, 50.0, 50.0) ];

label.text = @"Label Text";

label.textAlignment = UITextAlignmentLeft;

label.textColor = [ UIColor redColor ];

label.font = [ UIFont fontWithName: @"Arial" size: 10.0 ];

標簽label可以設置文本陰影,甚至可以定義陰影的偏移:

label.shadowColor = [ UIColor grayColor ];

label.shadowOffset = CGSizeMake(0, -1);

高亮是的顏色:

label.highlightedTextColor = [ UIColor blackColor ];

標簽的背景色:

label.backgroundColor = [ UIColor blueColor ];

把標簽加到cell里

[ cell addSubview: label ];

(8) 附件

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

Style

Description

UITableViewCellAccessoryNone

沒有附件

UITableViewCellAccessoryDisclosureIndicator

黑色向右的箭頭

UITableViewCellAccessoryDetailDisclosureButton

藍色附件按鈕

UITableViewCellAccessoryCheckmark

復選框,支持選擇

 

7.3 實現多選

- (void)tableView:(UITableView *)tableView

        didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

{

    NSLog(@"Selected section %d, cell %d", 

        [ indexPath indexAtPosition: 0 ], [ indexPath indexAtPosition: 1 ]); 

    //獲的當前選擇項

    UITableViewCell *cell = [ self.tableView cellForRowAtIndexPath: indexPath ]; 

    //顯示復選框

    if (cell.accessoryType == UITableViewCellAccessoryNone)

        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    else

        cell.accessoryType = UITableViewCellAccessoryNone; 

}

7.4 編輯和刪除

在允許用戶刪除和編輯的時候,每一個cell左邊會顯示一個紅色刪除圖標

[ self.tableView setEditing:YES animated:YES ];

關閉編輯的時候,table頂部會顯示一個Edit導航條

[ self.tableView setEditing: NO animated: YES ];

在編輯過程中,如果用戶要刪除該項,會彈出一個刪除確認框. 確認后調UITableViewDataSource類的commitEditingStyle方法來通知你的應用程序, 然后你可以從你的底層數據源里刪除該項,並通知table view刪除該行.

- (void)tableView:(UITableView *)tableView

    commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

    forRowAtIndexPath:(NSIndexPath *) indexPath 

{

    if (editingStyle == UITableViewCellEditingStyleDelete) 

    {

        NSLog(@"Deleted section %d, cell %d", [ indexPath indexAtPosition: 0 ], [ indexPath indexAtPosition: 1 ]);

        NSMutableArray *array = [ [ NSMutableArray alloc ] init ];

        [ array addObject: indexPath ];

        [ self.tableView deleteRowsAtIndexPaths: array

            withRowAnimation: UITableViewRowAnimationFade 

        ];

     }

}

通過傳遞一個數組給deleteRowsAtIndexPaths方法, 可以刪除一行或多行.

 

withRowAnimation至此下列預定義的刪除動畫

Animation

Description

UITableViewRowAnimationFade

Cell fades out

UITableViewRowAnimationRight

Cell slides out from right

UITableViewRowAnimationLeft  

Cell slides out from left

UITableViewRowAnimationTop

Cell slides out to top of adjacent cell

UITableViewRowAnimationBottom

Cell slides out to bottom of adjacent cell

 

7.5 重新加載表

當你的數據變了的時候,你可以重新加載整個表

[ self.tableView reloadData ];


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM