自定義UITableViewCell的背景

2021-06-08 07:47:40 字數 1920 閱讀 2402

uitableviewcell是乙個很常用的view,通常我們都是直接使用它。

-

(uitableviewcell *

)tableview:

(uitableview *

)tableview cellforrowatindexpath:

(nsindexpath *

)indexpath

cell.textlabel.text

=[nsstring stringwithformat:@"line: %d"

, indexpath.row];

return cell;

}

得到這個效果:

現在我們給tableviewcell加上點背景色:

-

(uitableviewcell *

)tableview:

(uitableview *

)tableview cellforrowatindexpath:

(nsindexpath *

)indexpath

cell.textlabel.text

=[nsstring stringwithformat:@"line: %d"

, indexpath.row];

// cell.backgroundcolor = [uicolor bluecolor];

cell.contentview.backgroundcolor

=[uicolor bluecolor]

;return cell;

}

我們不應該直接使用cell.backgroundcolor。cell本身是乙個uiview,我們所看到的部分其實只是它的乙個subview,也就是cell.contentview。所以,如果直接改變cell本身的背景色,依然會被cell.contentview給覆蓋,沒有效果。

不過,通過cell.contentview.backgroundcolor來改變背景色還不是最好的practice. 如果通過

tableview.editing

= yes;

進入edit模式,就會出現問題。

cocoa提供的按鈕背景色為透明。因為contentview被移開,下面是tableview的顏色,已經不是cell的一部分了。

所以,最好的方式應該是通過cell.backgroundview來改變cell的背景。按照文件說明,backgroundview始終處於cell的最下層,所以,將cell裡的其它subview背景設為[uicolor clearcolor],以cell.backgroundview作為統一的背景,應該是最好的方式。

-

(uitableviewcell *

)tableview:

(uitableview *

)tableview cellforrowatindexpath:

(nsindexpath *

)indexpath

cell.textlabel.text

=[nsstring stringwithformat:@"line: %d"

, indexpath.row];

cell.textlabel.backgroundcolor

=[uicolor clearcolor]; 

uiview *backgrdview =

[[uiview alloc] initwithframe:cell.frame];

backgrdview.backgroundcolor

=[uicolor bluecolor]

; cell.backgroundview

= backgrdview;

[backgrdview release]

;return cell;

}

效果:

自定義 如何自定義協議

何為自定義協議,其實是相對標準協議來說的,這裡主要針對的是應用層協議 常見的標準的應用層協議如http ftp smtp等,如果我們在網路通訊的過程中不去使用這些標準協議,那就需要自定義協議,比如我們常用的rpc框架 dubbo,thrift 分布式快取 redis,memcached 等都是自定義...

自定義控制項 自定義鐘錶

private context mcontext 畫筆 private paint mpaint 控制項的寬 private int mwidth x方向的圓心座標 private int center 鐘錶的半徑 private int mradio 圓環的寬 private int stroke...

自定義控制項及自定義屬性

自定義控制項在android開發中的重要性,是不言而喻,眾人皆知的。希望通過這二天的學習,能讓大家了解自定義控制項的原理,熟悉自定義控制項的使用步驟,並能寫出一些普通的效果。內容介紹 1 使用系統控制項,實現自定義的效果,案例有 優酷環形選單 廣告條 viewpager 下拉列表 spinner 2...