iOS常用代码,拿来即用

1、============= 获取UIButton在UITableviewCell的行数

==法1==

cellForRowAtIndexPath

UIButton *btnDelete = [[UIButton alloc] init];
btnDelete.frame = CGRectMake(267, 10, 46, 40);
[btnDelete addTarget:self action:@selector(btnDeleteAction:event:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btnDelete];
[btnDelete release];
UIButton方法

- (void)btnDeleteAction:(id)sender event:(id)event{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:_tableview];
    NSIndexPath *indexPath = [_tableview indexPathForRowAtPoint: currentTouchPosition];
}


==法2==

cellForRowAtIndexPath

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /**
     *  other code xxxxxxxxxxx
     */
    
    
    // Cell creation code here. “theButton” is the button, and “cell” is the cell.
    objc_setAssociatedObject(theButton, "cell", cell, OBJC_ASSOCIATION_ASSIGN);
    return cell;
}

UIButton方法

- (IBAction)myButtonPressed:(id)sender
{
    UITableViewCell *cell = objc_getAssociatedObject(sender, "cell");
    
    if (cell) {
        
    }
}

2、============= UILabel的根据字符串长度自适应大小

NSString *content = @"嘿施主,贫僧真的没有偷瞄你啊,表这样嘛";

UIFont *font = [UIFont systemFontOfSize:15.0];
CGSize size = CGSizeMake(320,2000);
CGSize labelsize = [content sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];

UILabel *lbContent = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, labelsize.width, labelsize.height)] autorelease];
lbContent.textColor = [UIColor blackColor];
lbContent.backgroundColor = [UIColor clearColor];
lbContent.numberOfLines = 0;
lbContent.lineBreakMode = NSLineBreakByWordWrapping;

3、============= 导航栏的设置

背景颜色

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

背景图片

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"test.png"] forBarMetrics:UIBarMetricsDefault];

按钮

- (void)edittingButton{
    UIButton *leftButton = [[ShareValues shareInstance] setRightBarButton];
    [leftButton setTitle:@"编辑" forState:UIControlStateNormal];
    [leftButton setTitle:@"编辑" forState:UIControlStateHighlighted];
    [leftButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0]];    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton] autorelease];
}

4、============= 获取时间戳

- (NSString *)getTimeStamp{
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formater setTimeZone:timeZone];
    NSString * curTime = [formater stringFromDate:[NSDate date]];
    
    return curTime;
}

5、============= 监测UITextField内容变化

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged:) name:UITextFieldTextDidChangeNotification object:_tf_VerifyCode];
}
- (void)textFieldChanged:(id)sender
{
    if (_tf_VerifyCode.text.length > 0){
        _btnGetSubPhone.userInteractionEnabled = YES;
        [_btnGetSubPhone setTitleColor:[UIColor colorWithRed:50/255.0 green:79/255.0 blue:133/255.0 alpha:1.0] forState:UIControlStateNormal];
        [_btnGetSubPhone setTitleColor:[UIColor colorWithRed:50/255.0 green:79/255.0 blue:133/255.0 alpha:1.0] forState:UIControlStateHighlighted];
    }else{
        _btnGetSubPhone.userInteractionEnabled = NO;
        [_btnGetSubPhone setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_btnGetSubPhone setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
    [super dealloc];
}

6、============= 设置圆形UIImageView

UIImageView *imgvReward = [[UIImageView alloc] initWithFrame:CGRectMake(280, 15, 30, 30)];
[imgvReward setBackgroundColor:[UIColor colorWithRed:98/255.0 green:173/255.0 blue:250/255.0 alpha:1.0]];
imgvReward.layer.cornerRadius = imgvReward.frame.size.width / 2;
imgvReward.layer.masksToBounds = YES;






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值