前言:cell那點(diǎn)事兒
原生的tableview的樣式那幾種或許你會覺得這根本不夠用啊腔剂,然而有時候的確又沒有必要再去大肆的浪費(fèi)時間去創(chuàng)建萬金油cell。這個時候我們看看源碼里的兩個屬性。
@property (nonatomic, readonly) UITableViewCellEditingStyle editingStyle;
// default is UITableViewCellEditingStyleNone. This is set by UITableView using the delegate's value for cells who customize their appearance accordingly.
@property (nonatomic) BOOL showsReorderControl; // default is NO
@property (nonatomic) BOOL shouldIndentWhileEditing; // default is YES. This is unrelated to the indentation level below.
@property (nonatomic) UITableViewCellAccessoryType accessoryType; // default is UITableViewCellAccessoryNone. use to set standard type
@property (nonatomic, strong, nullable) UIView *accessoryView; // if set, use custom view. ignore accessoryType. tracks if enabled can calls accessory action
@property (nonatomic) UITableViewCellAccessoryType editingAccessoryType; // default is UITableViewCellAccessoryNone. use to set standard type
@property (nonatomic, strong, nullable) UIView *editingAccessoryView; // if set, use custom view. ignore editingAccessoryType. tracks if enabled can calls accessory action
如果你的項目匯總有如此的需要规哪,只需要把右側(cè)默認(rèn)的剪頭改成圖片烁落,那么你來對了。
UIButton *btDele = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 27, 27)];
// btDele.backgroundColor = MTOther_Color;
[btDele setImage:[UIImage imageNamed:@"Trash"] forState:UIControlStateNormal];
btDele.userInteractionEnabled = NO;
// [btDele addTarget:self action:@selector(deletaPassenger) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = btDele;
這里我的需求是把Button的事件禁止掉膜赃,只響應(yīng)cell的挺邀,當(dāng)然你也可以不取消,都是可以的跳座。
你可以用任何UIView子類控件例如UILabel端铛、UIButton。疲眷。禾蚕。對象來給cell.accessoryView賦值,這樣界面上面就會顯示你想要的效果了狂丝。
設(shè)置UITableViewCell的accessoryType
如果希望cell上面顯示一個淺灰色的箭頭换淆,可以通過accessoryType來達(dá)到目的哗总,代碼如下,
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
這樣就設(shè)置了箭頭裝的type倍试,我們可以設(shè)置多種類型讯屈,蘋果定義的枚舉類型如下,
typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {
UITableViewCellAccessoryNone, //不顯示任何的accessoryView
UITableViewCellAccessoryDisclosureIndicator, //淺灰色箭頭圖標(biāo)
UITableViewCellAccessoryDetailDisclosureButton, //顯示詳情的按鈕
UITableViewCellAccessoryCheckmark, //就是你考試時候打鉤的鉤形狀
UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) //
};
讀者可以逐個試一試易猫,找到自己想要的效果耻煤。
有時候點(diǎn)擊cell時候加深的效果
設(shè)置UITableViewCell的點(diǎn)擊風(fēng)格selectionStyle,
有的時候我們需要點(diǎn)擊cell時候相應(yīng)的cell背景加深的效果准颓,有的時候我們不需要哈蝇,這時候可以使用下面的語句來實現(xiàn),
settingCell.selectionStyle = UITableViewCellSelectionStyleNone
查看文檔中的枚舉類型如下攘已,
typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray,
UITableViewCellSelectionStyleDefault NS_ENUM_AVAILABLE_IOS(7_0)
};