【IOS】UITableView樣式的自定義

不少時候,咱們須要自定義UITableView來知足咱們的特殊要求。這時候,關於UITableView和cell的自定義和技巧太多了,就須要不斷的總結和概括。
 
1.添加自定義的Cell。
 
這個問題已經涉及過,可是,這裏要說的主要是兩種方法的比較!
由於,我常常發現有兩種方式:
1.xib方式
這種方式,也就是說,爲自定義的UITableViewCell類添加一個xib的文件。而且讓二者關聯。
這時候,寫法爲:

 

// 返回cellhtml

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{app

    

    static NSString *CellIdentifier = @"MyCell";ide

    // 自定義cell函數

    MyCell *cell = (MyCell *)[tableVie dequeueReusableCellWithIdentifier:CellIdentifier];佈局

    if (cell == nil){學習

        // 這種方式,將會查找響應的xib文件,將不會調用initWithStyle方法動畫

        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:niloptions:nil];atom

        cell = [array objectAtIndex:0];url

    }spa

這種方式,是讀取了xib文件,因此,就直接按照響應的xib中的佈局,佈局好了,並不會調用相應的initWithStyle方法。
 
 
2.調用initWithStyle方法

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

    static NSString *CellIdentifier = @"MyCell";

    // 自定義cell

    MyCell *cell = (MyCell *)[tableVie dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil){

        // 這種方式,將會調用cell中的initWithStyle方法

        cell = [[[MyCell alloc] initWithStyle:UITableViewCellSelectionStyleGray reuseIdentifier:CellIdentifier] autorelease];

    }

    return cell;

    

}

 
這種方式,會調用相應Cell類的initWithStyle方法。
 
那麼,何時,用那種方式呢?
個人理解是:
當,cell比較簡單時,能夠添加相應的xib文件,進行關聯;當cell比較複雜時,就直接用純代碼的方式(不建立相應的xib文件)。
我發現,我仍是喜歡用純代碼的方式來寫,由於,擴展性好,尤爲當cell元素複雜甚至帶有動畫效果的時候,用xib反而很難控制,或者根本沒法控制。
我建議用純代碼的方式!
 
 
2.設置cell的setAccessoryView屬性
 
主要用在:在右邊添加一個自定義的按鈕,或者子視圖。
爲setAccessoryView設置一個按鈕。
 

cell.accessoryType = UITableViewCellAccessoryNone;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    

    [button setFrame:CGRectMake(0.0, 0.0, 55, 57)];

    [button setImage:[UIImage imageNamed:@"tap_normal.png"]forState:UIControlStateNormal];

    [button setImage:[UIImage imageNamed:@"tap_highlight.png"]forState:UIControlStateHighlighted];

    [button setTag:indexPath.row];

    [button addTarget:self action:@selector(doClickPlaybillAction:event:) forControlEvents:UIControlEventTouchUpInside];

 

    [button setBackgroundColor:[UIColor clearColor]];

    [cell setAccessoryView:button];

 

    return cell;

 
經過觀察屬性定義:

@property(nonatomic) UITableViewCellAccessoryType   accessoryType;              

@property(nonatomic,retain) UIView                 *accessoryView;              

@property(nonatomic) UITableViewCellAccessoryType   editingAccessoryType;       

@property(nonatomic,retain) UIView                 *editingAccessoryView; 

可見,accessoryView屬性須要的參數爲UIView,因此,能夠很方便的自定義。固然還有editingAccessoryView,能夠進行自定義修改時的UIView。
 
根據用戶點擊的按鈕,找到相應的Cell
 

- (void) performExpand:(id)paramSender{

 

    UITableViewCell *ownerCell = (UITableViewCell*)[paramSender superview];// 得到父視圖,即TableViewCell

    if (ownerCell != nil){

 

        NSIndexPath *ownerCellIndexPath = [self.myTableView indexPathForCell:ownerCell];

        NSLog(@"Accessory in index path is tapped. Index path = %@", ownerCellIndexPath);

        

    }

}

 
3.自定義cell選擇時的樣式。

 

經過,上面一步,咱們爲Cell添加了一個自定義的按鈕。

也許就會遇到這麼一個糾結的狀況,當點擊UITableViewCell高亮時,其子視圖中不應高亮的對象(好比說自定義的那個按鈕)也高亮了。

 

好比:

正確方式:咱們須要cell被選中時,按鈕不該該也被高亮顯示。如:

 

UITableView <wbr>應用(五)UITableView樣式的自定義

 

錯誤方式:可是,cell被選中時,按鈕卻也高亮顯示了。如:

 

UITableView <wbr>應用(五)UITableView樣式的自定義

 

要解決該方法,能夠這樣:
 

 

 

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{

    [super setHighlighted:highlighted animated:animated];

    

    if(highlighted) {

        [(UIButton *)self.accessoryView setHighlighted:NO];

    }

}

 

 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{

    [super setSelected:selected animated:animated];

    if(selected) {

        [(UIButton *)self.accessoryView setHighlighted:NO];

    }

}

 
這樣,問題時解決了,那若是咱們再深一層次,發問一下:
 
爲何UITableViewCell被選中時,UITableViewCell中的其餘元素也會被高亮顯示呢?
 

由於當UITableViewCell爲選中狀態時,UITableViewCell把selectedBackgroundView看成一個子視圖來添加;

selectedBackgroundView被添加在UITableViewCell的backgroundView之上,或者全部其它視圖之下。

當調用setSelected: animated:這一方法時,會致使selectedBackgroundView以一個alpha消化的狀態來出現和消失。

還應該注意:

UITableViewCell的selectionStyle值爲UITableViewCellSelectionStyleNone時,selectedBackgroundView將不起做用。

 
 
 
 
4.爲UITableViewCell添加自定義背景
 
有時候,咱們要爲UITableViewCell自定義的類的每一個cell添加自定義的背景圖片。
有不少方法:
1.在自定義的UITableViewCell類的initWithStyle方法中,添加以下代碼:

// 設置背景

        UIImageView *bgImage=[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0,320, 57)] autorelease];

        [bgImage setImage: [UIImage imageNamed:@"table_live_bg.png"]];

        [self setBackgroundView:bgImage];

 
2.使用setBackgroundImageByName方法或者setBackgroundImage方法
 

[self setBackgroundImageByName:@"table_live_bg.png"];

[self setBackgroundImage:[UIImage imageNamed:@"table_live_bg.png"]];

這種方法,要注意的時,設置的圖片大小應該與cell大小相同
 
3.設置cell的contentView,用insertSubview方法
 

[self.contentView insertSubview:messageBackgroundViewbelowSubview:self.textLabel];

        

 self.selectionStyle = UITableViewCellSelectionStyleNone;

 
這三種方式,都在initWithStyle方法中設置。
 
5.設置刪除Cell時的自定義文本
 

//定製Delete字符串,添加函數 返回要顯示的字符串

-(NSString *)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

    return @"刪除";

}

 
 本文轉載自:http://blog.sina.com.cn/s/blog_7b9d64af0101aaoy.html
僅供學習參考
相關文章
相關標籤/搜索