亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS App開(kāi)發(fā)中使用及自定義UITableViewCell的教程

 更新時(shí)間:2016年04月19日 09:13:51   作者:張智清  
這篇文章主要介紹了iOS App開(kāi)發(fā)中使用及自定義UITableViewCell的教程,自定義TableViewCell文中使用Objective-C演示而非ib,需要的朋友可以參考下

UITableView用來(lái)以表格的形式顯示數(shù)據(jù)。關(guān)于UITableView,我們應(yīng)該注意:

(1)UITableView用來(lái)顯示表格的可見(jiàn)部分,UITableViewCell用來(lái)顯示表格的一行。

(2)UITableView并不負(fù)責(zé)存儲(chǔ)表格中的數(shù)據(jù),而是僅僅存儲(chǔ)足夠的數(shù)據(jù)使得可以畫出當(dāng)前可見(jiàn)部分。

(3)UITableView從UITableViewDelegate協(xié)議獲取配置信息,從UITableViewDataSource協(xié)議獲得數(shù)據(jù)信息。

(4)所有的UITableView實(shí)現(xiàn)時(shí)實(shí)際上只有一列,但是我們可以通過(guò)向UITableViewCell中添加子視圖,使得它看起來(lái)有好幾列。

(5)UITableView有兩種風(fēng)格:

    ① Grouped:每一組看起來(lái)像是圓矩形;
    ② Plain:這是默認(rèn)風(fēng)格,可以修改成Indexed風(fēng)格。
   
UITableViewCell使用實(shí)例
在下邊的小例子中,我們將先實(shí)現(xiàn)顯示一列數(shù)據(jù),然后在每行添加圖像,之后再看看UITableViewCell的四種分別是什么樣的。最后再進(jìn)行其他操作,比如設(shè)置縮進(jìn)、修改字體大小和行高等。

1、運(yùn)行Xcode 4.2,新建一個(gè)Single View Application,名稱為Table Sample:

201641990909065.png (475×194)

2、單擊ViewController.xib,使用Interface Builder給視圖添加一個(gè)UITableView控件,并使其覆蓋整個(gè)視圖:

201641990939756.jpg (951×510)

3、選中新添加的UITableView控件,打開(kāi)Connection Inspector,找到delegate和datasource,從它們右邊的圓圈拉線到File's Owner圖標(biāo):

201641990956899.jpg (837×192)

4、單擊ViewController.h,在其中添加代碼:

復(fù)制代碼 代碼如下:

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) NSArray *listData;
@end

5、單擊ViewController.m,在其中添加代碼:

5.1 在@implementation后面添加代碼:

復(fù)制代碼 代碼如下:

@synthesize listData;

5.2 在viewDidLoad方法中添加代碼:
復(fù)制代碼 代碼如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSArray *array = [[NSArray alloc] initWithObjects:@"Tree", @"Flower",
                      @"Grass", @"Fence", @"House", @"Table", @"Chair",
                      @"Book", @"Swing" , nil];
    self.listData = array;
}

5.3 在viewDidUnload方法中添加代碼:
復(fù)制代碼 代碼如下:

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.listData = nil;
}

5.4 在@end之前添加代碼:
復(fù)制代碼 代碼如下:

#pragma mark -
#pragma mark Table View Data Source Methods
//返回行數(shù)
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
    return [self.listData count];
}

//新建某一行并返回
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             TableSampleIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:TableSampleIdentifier];
    }
   
    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];
 return cell;
}


上面的第二個(gè)方法中,
復(fù)制代碼 代碼如下:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier];

這個(gè)語(yǔ)句根據(jù)標(biāo)識(shí)符TableSampleIdentifier尋找當(dāng)前可以重用的UITableViewCell。當(dāng)某行滑出當(dāng)前可見(jiàn)區(qū)域后,我們重用它所對(duì)應(yīng)的UITableViewCell對(duì)象,那么就可以節(jié)省內(nèi)存和時(shí)間。
如果執(zhí)行詞語(yǔ)后,cell為nil,那我們?cè)賱?chuàng)建一個(gè),并設(shè)置去標(biāo)識(shí)符為TableSampleIdentifier:
復(fù)制代碼 代碼如下:

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableSampleIdentifier];

這里UITableViewCellStyleDefault是表示UITableViewCell風(fēng)格的常數(shù),除此之外,還有其他風(fēng)格,后面將會(huì)用到。
注意參數(shù)(NSIndexPath *)indexPath,它將行號(hào)row和部分號(hào)section合并了,通過(guò)[indexPath row];獲取行號(hào)。之后給cell設(shè)置其文本:
復(fù)制代碼 代碼如下:

cell.textLabel.text = [listData objectAtIndex: row];

6、運(yùn)行一下:

201641991022472.png (284×405)

7、給每一行添加圖片:
7.1 將圖片資源添加到工程:拖到工程中,前面的文章有提到。
7.2 在cellForRowAtIndexPath方法的return語(yǔ)句之前添加代碼:

復(fù)制代碼 代碼如下:

UIImage *image = [UIImage imageNamed:@"blue.png"];
cell.imageView.image = image;
UIImage *highLighedImage = [UIImage imageNamed:@"yellow.png"];
cell.imageView.highlighedImage = highLighedImage;

7.3 運(yùn)行,效果如下:

201641991042257.png (284×405)

可以看到,每行左邊都出現(xiàn)一張圖片。當(dāng)選中某行,其圖片改變。

8、設(shè)置行的風(fēng)格:
表示UITableViewCell風(fēng)格的常量有:

復(fù)制代碼 代碼如下:

UITableViewCellStyleDefault
UITableViewCellStyleSubtitle
UITableViewCellStyleValue1
UITableViewCellStyleValue2

這幾種風(fēng)格的區(qū)別主要體現(xiàn)在Image、Text Label以及Detail Text Label。
為了體現(xiàn)風(fēng)格,在cellForRowAtIndexPath方法的return語(yǔ)句之前添加代碼:
復(fù)制代碼 代碼如下:

cell.detailTextLabel.text = @"Detail Text";

然后將
復(fù)制代碼 代碼如下:

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableSampleIdentifier];

中的UITableViewCellStyleDefault依次換成上面提到的四個(gè)風(fēng)格常量,并運(yùn)行,效果分別如下:

201641991102458.png (288×120)

UITableViewCellStyleDefault  

201641991118771.png (288×120)

UITableViewCellStyleSubtitle

201641991134672.png (288×120)

UITableViewCellStyleValue1

201641991151227.png (288×120)

UITableViewCellStyleValue2
    
9、設(shè)置縮進(jìn):

將所有行的風(fēng)格改回UITableViewCellStyleDefault,然后在@end之前添加代碼如下:

復(fù)制代碼 代碼如下:

#pragma mark Table Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    return row;
}

這里將第row行縮進(jìn)row,如下圖所示:

201641991212833.png (280×400)

10、操縱行選擇:
在@end之前添加代碼:

復(fù)制代碼 代碼如下:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    if (row%2 == 0) {
        return nil;
    }
    return indexPath;
}

上面的方法在選擇某行之前執(zhí)行,我們可以在這個(gè)方法中添加我們想要的操作。這里,我們實(shí)現(xiàn)的是,如果選擇的行號(hào)(從0開(kāi)始計(jì))是偶數(shù),則取消選擇。
在@end之前添加代碼:
復(fù)制代碼 代碼如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    NSString *rowValue = [listData objectAtIndex:row];
   
    NSString *message = [[NSString alloc] initWithFormat:
                         @"You selected %@", rowValue];
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Row Selected!"
                          message:message
                          delegate:nil
                          cancelButtonTitle:@"Yes I Did"
                          otherButtonTitles:nil];
    [alert show];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

當(dāng)選擇某行之后,就彈出一個(gè)Alert,用來(lái)顯示我們所做的選擇。
運(yùn)行一下,你會(huì)發(fā)現(xiàn)第0、2等行無(wú)法選擇。選擇奇數(shù)行時(shí)會(huì)彈出提示:

201641991230749.jpg (280×400)

而且關(guān)閉提示框后,選擇的那行也被取消了選擇,用的語(yǔ)句

復(fù)制代碼 代碼如下:

[tableView deselectRowAtIndexPath:indexPath animated:YES];

11、設(shè)置字體大小和表格行高:
11.1 在cellForRowAtIndexPath方法中的return之前添加代碼,用于設(shè)置字體和大小:
復(fù)制代碼 代碼如下:

cell.textLabel.font = [UIFont boldSystemFontOfSize:50];

11.2 在@end之前添加代碼,用于設(shè)置行高:
復(fù)制代碼 代碼如下:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

運(yùn)行,看看效果:

201641991250465.png (259×171)

可任意自定義的UITableViewCell
UITableView的強(qiáng)大更多程度上來(lái)自于可以任意自定義UITableViewCell單元格。通常,UITableView中的Cell是動(dòng)態(tài)的,在使用過(guò)程中,會(huì)創(chuàng)建一個(gè)Cell池,根據(jù)每個(gè)cell的高度(即tableView:heightForRowAtIndexPath:返回值),以及屏幕高度計(jì)算屏幕中可顯示幾個(gè)cell。而進(jìn)行自定義TableViewCell無(wú)非是采用代碼實(shí)現(xiàn)或采用IB編輯nib文件來(lái)實(shí)現(xiàn)兩種方式,本文主要收集代碼的方式實(shí)現(xiàn)各種cell自定義。
1.如何動(dòng)態(tài)調(diào)整Cell高度

復(fù)制代碼 代碼如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    static NSString *CellIdentifier = @"Cell";
 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.tag = 1;
        label.lineBreakMode = UILineBreakModeWordWrap;
        label.highlightedTextColor = [UIColor whiteColor];
        label.numberOfLines = 0;
        label.opaque = NO; // 選中Opaque表示視圖后面的任何內(nèi)容都不應(yīng)該繪制
        label.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:label];
        [label release];
    }
 
    UILabel *label = (UILabel *)[cell viewWithTag:1];
    NSString *text;
    text = [textArray objectAtIndex:indexPath.row];
    CGRect cellFrame = [cell frame];
    cellFrame.origin = CGPointMake(0, 0);
 
    label.text = text;
    CGRect rect = CGRectInset(cellFrame, 2, 2);
    label.frame = rect;
    [label sizeToFit];
    if (label.frame.size.height > 46) {
        cellFrame.size.height = 50 + label.frame.size.height - 46;
    }
    else {
        cellFrame.size.height = 50;
    }
    [cell setFrame:cellFrame];
 
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}


2.如何用圖片自定義Table Separeator分割線
一般地,利用類似[tableView setSeparatorColor:[UIColor redColor]];語(yǔ)句即可修改cell中間分割線的顏色。那又如何用一個(gè)圖片作為分割線背景呢?可以嘗試如下:
方法一:
先設(shè)置cell separatorColor為clear,然后把圖片做的分割線添加到自定義的custom cell上。

方法二:
在cell里添加一個(gè)像素的imageView后將圖片載入進(jìn),之后設(shè)置tableView.separatorStyle = UITableViewCellSeparatorStyleNone

3.自定義首行Cell與其上面導(dǎo)航欄間距

復(fù)制代碼 代碼如下:

tableView.tableHeaderView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,5,20)] autorelease];

4.自定義UITableViewCell的accessory樣式
默認(rèn)的accessoryType屬性有四種取值:UITableViewCellAccessoryNone、UITableViewCellAccessoryDisclosureIndicator、UITableViewCellAccessoryDetailDisclosureButton、UITableViewCellAccessoryCheckmark。如果想使用自定義附件按鈕的其他樣式,則需使用UITableView的accessoryView屬性來(lái)指定。
復(fù)制代碼 代碼如下:

UIButton *button;
if(isEditableOrNot) {
    UIImage *image = [UIImage imageNamed:@"delete.png"];
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0.0,0.0,image.size.width,image.size.height);
    button.frame = frame;
    [button setBackgroundImage:image forState:UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;
}else{
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;
}

以上代碼僅僅是定義了附件按鈕兩種狀態(tài)下的樣式,問(wèn)題是現(xiàn)在這個(gè)自定義附件按鈕的事件仍不可用。即事件還無(wú)法傳遞到UITableViewDelegate的accessoryButtonTappedForRowWithIndexPath方法上。當(dāng)我們?cè)谏鲜龃a中在加入以下語(yǔ)句:
復(fù)制代碼 代碼如下:
[button addTarget:self action:@selector(btnClicked:event:) forControlEvents:UIControlEventTouchUpInside];

后,雖然可以捕捉到每個(gè)附件按鈕的點(diǎn)擊事件,但我們還無(wú)法進(jìn)行區(qū)別到底是哪一行的附件按鈕發(fā)生了點(diǎn)擊動(dòng)作!因?yàn)閍ddTarget:方法最多允許傳遞兩個(gè)參數(shù):target和event,這兩個(gè)參數(shù)都有各自的用途了(target指向事件委托對(duì)象,event指向所發(fā)生的事件)??磥?lái)只依靠Cocoa框架已經(jīng)無(wú)法做到了。

      但我們還是可以利用event參數(shù),在自定義的btnClicked方法中判斷出事件發(fā)生在UITableView的哪一個(gè)cell上。因?yàn)閁ITableView有一個(gè)很關(guān)鍵的方法indexPathForRowAtPoint,可以根據(jù)觸摸發(fā)生的位置,返回觸摸發(fā)生在哪一個(gè)cell的indexPath。而且通過(guò)event對(duì)象,正好也可以獲得每個(gè)觸摸在視圖中的位置。

復(fù)制代碼 代碼如下:

// 檢查用戶點(diǎn)擊按鈕時(shí)的位置,并轉(zhuǎn)發(fā)事件到對(duì)應(yīng)的accessory tapped事件
- (void)btnClicked:(id)sender event:(id)event
{
     NSSet *touches = [event allTouches];
     UITouch *touch = [touches anyObject];
     CGPoint currentTouchPosition = [touch locationInView:self.tableView];
     NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
     if(indexPath != nil)
     {
         [self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
     }
}

這樣,UITableView的accessoryButtonTappedForRowWithIndexPath方法會(huì)被觸發(fā),并且獲得一個(gè)indexPath參數(shù)。通過(guò)這個(gè)indexPath參數(shù),我們即可區(qū)分到底哪一行的附件按鈕發(fā)生了觸摸事件。
復(fù)制代碼 代碼如下:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    int  *idx = indexPath.row;
   //這里加入自己的邏輯
}


相關(guān)文章

最新評(píng)論