UItableViewCell一些实用细节与技巧

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];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值