您的位置:首页 > 移动开发 > IOS开发

iOS小技巧总结(持续更新)

2016-11-07 08:49 399 查看
1.在tableView中点击cell后,cell的颜色会默认显示为灰色,在tableView的delegate的didSelectRowAtIndexPath方法中加入[tableView deselectRowAtIndexPath:indexPath animated:YES];即可完美解决,示例如下:

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

[tableView deselectRowAtIndexPath:indexPath animated:YES];

...
}


2.tableView的separatorStyle和separatorColor属性

我们可以通过设置tableView的separatorStyle属性来设置有无分割线以及分割线的风格,其中style定义如下:

typedef enum {
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine,
UITableViewCellSeparatorStyleSingleLineEtched
} UITableViewCellSeparatorStyle;


同时还可以通过tableView的separatorColor属性来设置分割线的颜色。

如果自行设计的话可以在自定义的cell中加入如下代码:

// cell分割线
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, rect);

//上分割线,
CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:@"ffffff"].CGColor);
CGContextStrokeRect(context, CGRectMake(5, -1, rect.size.width - 10, 1));

//下分割线
CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:@"e2e2e2"].CGColor);
CGContextStrokeRect(context, CGRectMake(5, rect.size.height, rect.size.width - 10, 1));
}


3.去除UITextField两端空格和回车,详细查看该文章:http://nshipster.cn/nscharacterset/

NSString *mobile = _vMobileText.text;
NSString *password = _vPasswordText.text;

//去除两端空格
mobile = [mobile stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
password = [password stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//去除两端空格和回车
mobile = [mobile stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
password = [password stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


4.判断手机号和密码

//自定义错误提供给NSError使用
#define kCustomErrorDomain @"com.ETShop-for-iOS.ios"
typedef enum {
eCustomErrorCodeFailure = 0
} eCustomErrorCode;


//判断是否正确的手机号码
+ (BOOL)isValidMobile:(NSString *)mobile error:(NSError **)error;
{
NSDictionary *errorUserInfo;

if (mobile.length <= 0) {
errorUserInfo = [NSDictionary dictionaryWithObject:@"请输入手机号"                                                                      forKey:NSLocalizedDescriptionKey];
*error = [NSError errorWithDomain:kCustomErrorDomain code:eCustomErrorCodeFailure userInfo:errorUserInfo];
return NO;
}

if (mobile.length != 11) {
errorUserInfo = [NSDictionary dictionaryWithObject:@"手机号长度只能是11位"                                                                      forKey:NSLocalizedDescriptionKey];
*error = [NSError errorWithDomain:kCustomErrorDomain code:eCustomErrorCodeFailure userInfo:errorUserInfo];
return NO;
} else {
/**
* 移动号段正则表达式
*/
NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
/**
* 联通号段正则表达式
*/
NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
/**
* 电信号段正则表达式
*/
NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
BOOL isMatch3 = [pred3 evaluateWithObject:mobile];

if (isMatch1 || isMatch2 || isMatch3) {
return YES;
} else {
errorUserInfo = [NSDictionary dictionaryWithObject:@"请输入正确的电话号码"                                                                      forKey:NSLocalizedDescriptionKey];
*error = [NSError errorWithDomain:kCustomErrorDomain code:eCustomErrorCodeFailure userInfo:errorUserInfo];
return NO;
}
}
}

//判断是否正确的密码格式
+ (BOOL)isValidPassword:(NSString *)password error:(NSError **)error
{
NSDictionary *errorUserInfo;

if (password.length < 6) {
errorUserInfo = [NSDictionary dictionaryWithObject:@"密码长度必须六位以上" forKey:NSLocalizedDescriptionKey];
*error = [NSError errorWithDomain:kCustomErrorDomain code:eCustomErrorCodeFailure userInfo:errorUserInfo];
return NO;
}

return YES;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS小技巧总结