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

iOS代码常用的代码片段

2015-08-31 12:16 387 查看
1、列表回到顶部

[self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];


2、取消列表选择状态

self.selectionStyle = UITableViewCellSelectionStyleNone;


3、数组倒序

arrData = [[arr reverseObjectEnumerator] allObjects];


4、tableview隐藏多余的分割线

//tableview没有数据的时候不显示线
_tableView.tableFooterView = [[UIView alloc] init];


5、隐藏tableView分割线

tableView.separatorStyle = UITableViewCellSeparatorStyleNone


6、常用宏定义

//获取屏幕 宽度、高度
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height


7、提示框

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:alertAction];
[self.parentViewController presentViewController:alertController animated:YES completion:nil];


8、网络请求

//使用NSURLSessionDataTask请求数据
- (void)loadData {

// 创建Data Task
NSURL *url = [NSURL URLWithString:@"http://192.168.41.48/~jenkins/Json/lottery.php"];

NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
// 输出返回的状态码,请求成功的话为200
NSDictionary *jsondic;
jsondic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@",jsondic);

num = [[[jsondic objectForKey:@"result"] objectForKey:@"num"] integerValue];

[self startAnimation];

}];
[dataTask resume];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios