1.加上这句代码就没有一直选中状态了,只在点击时有个状态
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
2.UITableViewCell可以选择但无选择状态
cell.selectionStyle = UITableViewCellSelectionStyleNone;
状态有以下几种
typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray,
UITableViewCellSelectionStyleDefault NS_ENUM_AVAILABLE_IOS(7_0)
};
3.UITableViewCell设为不可选中(会影响一些设置)
cell.userInteractionEnabled = NO;
4.UITableViewCell设成选中状态
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section] animated:NO scrollPosition:UITableViewScrollPositionNone];
5.UITableViewCell在右侧添加Switch(开关)
可以自己先定义一个开关,然后直接加到cell的accessoryView上即可
-(UISwitch *)blockSwitch
{
if (_blockSwitch == nil) {
_blockSwitch = [[UISwitch alloc]init];
_blockSwitch.onTintColor = RGBCOLOR(63, 125, 194);
[_blockSwitch addTarget:self action:@selector(blockSwitchChanged:) forControlEvents:UIControlEventValueChanged];
return _blockSwitch;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
......省略
cell.textLabel.text = @"消息免打扰";
cell.accessoryView = self.blockSwitch; //switch
}
6.Cell的下划线
cell下划线默认左侧有一段空白,想改变下划线可以用下面代码
//在viewDidLoad中
-(void)viewDidLoad
{
if ([mTableView respondsToSelector:@selector(setSeparatorInset:)]) {
[mTableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([mTableView respondsToSelector:@selector(setLayoutMargins:)]) {
[mTableView setLayoutMargins:UIEdgeInsetsZero];
}
}
//在tableView的代理方法中
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==5){
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}else{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsMake(0,10,0,0)];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsMake(0,10,0,0)];
}}
}
//这样第row == 5的cell里线就顶到头了
7.在常规布局情况下,cell的宽高固定为(320,44)
在cell里面重写setFrame
//处理宽高不跟随设置的问题
- (void)setFrame:(CGRect)frame
{
frame.size = CGSizeMake(LH_ScreenWidth, 75);
[super setFrame:frame];
}