日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

可任意自定義的UITableViewCell

系統(tǒng) 2802 0

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

如何動態(tài)調(diào)整Cell高度

      - (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;
}

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

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

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

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

自定義UITableViewCell的accessory樣式
? ? ? 默認(rèn)的accessoryType屬性有四種取值:UITableViewCellAccessoryNone、UITableViewCellAccessoryDisclosureIndicator、UITableViewCellAccessoryDetailDisclosureButton、UITableViewCellAccessoryCheckmark。如果想使用自定義附件按鈕的其他樣式,則需使用UITableView的 accessoryView 屬性來指定。

      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)下的樣式,問題是現(xiàn)在這個(gè)自定義附件按鈕的事件仍不可用。即事件還無法傳遞到UITableViewDelegate的accessoryButtonTappedForRowWithIndexPath方法上。當(dāng)我們在上述代碼中在加入以下語句:
? ? ? ? ? ? ? [button addTarget:self action:@selector(btnClicked:event:) forControlEvents:UIControlEventTouchUpInside];
后,雖然可以捕捉到每個(gè)附件按鈕的點(diǎn)擊事件,但我們還無法進(jìn)行區(qū)別到底是哪一行的附件按鈕發(fā)生了點(diǎn)擊動作!因?yàn)閍ddTarget:方法最多允許傳遞兩個(gè)參數(shù):target和event,這兩個(gè)參數(shù)都有各自的用途了(target指向事件委托對象,event指向所發(fā)生的事件)。看來只依靠Cocoa框架已經(jīng)無法做到了。

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

      
        //
      
      
         檢查用戶點(diǎn)擊按鈕時(shí)的位置,并轉(zhuǎn)發(fā)事件到對應(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方法會被觸發(fā),并且獲得一個(gè)indexPath參數(shù)。通過這個(gè)indexPath參數(shù),我們即可區(qū)分到底哪一行的附件按鈕發(fā)生了觸摸事件。

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

可任意自定義的UITableViewCell


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 连南| 祥云县| 河西区| 闽侯县| 宜都市| 沙田区| 台湾省| 防城港市| 景洪市| 德庆县| 基隆市| 永德县| 隆安县| 镇雄县| 邵东县| 泰来县| 迭部县| 平舆县| 襄汾县| 巴塘县| 穆棱市| 龙里县| 开原市| 汉川市| 枣阳市| 漳平市| 乃东县| 澄城县| 海原县| 屯留县| 宜兴市| 万载县| 湖州市| 金华市| 化隆| 沿河| 子洲县| 苏州市| 沙雅县| 郧西县| 神池县|