iOS oc-cell總結

1.系統默認的顏色設置

//無色

cell.selectionStyle = UITableViewCellSelectionStyleNone;

//藍色

cell.selectionStyle = UITableViewCellSelectionStyleBlue;

//灰色

cell.selectionStyle = UITableViewCellSelectionStyleGray;

2.自定義顏色和背景設置

改變UITableViewCell選中時背景色:

cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];

cell.selectedBackgroundView.backgroundColor = [UIColor xxxxxx];

3.自定義UITableViewCell選中時背景

cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellart.png"]] autorelease];

還有字體顏色

cell.textLabel.highlightedTextColor = [UIColor xxxcolor];? [cell.textLabel setTextColor;

4.設置tableViewCell間的分割線的顏色

[theTableView setSeparatorColor:[UIColor xxxx ]];

5.pop返回table時,cell自動取消選中狀態

首先我有一個UITableViewController,其中每個UITableViewCell點擊后都會push另一個ViewController,每次點擊Cell的時候,Cell都會被選中,當從push的ViewController返回的時候選中的Cell便會自動取消選中。后來由于某些原因我把這個UITableViewController改成了UIViewController,之后就產生了一個問題:每次返回到TableView的時候,之前選中的Cell不能自動取消選中,經過查找得知:

UITableViewController有一個clearsSelectionOnViewWillAppear的property,

而當把UITableViewController修改成UIViewController后,這個屬性自然就不存在了,因此我們必須手動添加取消選中的功能,方法很簡單,在viewWillAppear方法中加入:

[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];

6.點擊后,過段時間cell自動取消選中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

…………

//消除cell選擇痕跡

[self performSelector:@selector(deselect) withObject:nil afterDelay:0.5f];

}

- (void)deselect

{

[self.tableview deselectRowAtIndexPath:[self.tableview indexPathForSelectedRow] animated:YES];

}

======加載xib自定cell=====

xxxxx *cell = [tableView dequeueReusableCellWithIdentifier:@"xxxxx"];

if(cell == nil){

cell = [[[NSBundle mainBundle] loadNibNamed:@"xxxxx" owner:self options:nil] objectAtIndex:0];

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容