其他分享
首页 > 其他分享> > IOS开发学习周报(三)

IOS开发学习周报(三)

作者:互联网

IOS开发学习周报(三)

简介

课程名称 IOS开发实训 任课老师 郑贵锋老师&字节跳动工程师
学号 16340015 专业(方向) 软件工程(计应)
姓名 陈彬彬 Email 944131226@qq.com
开始日期 2019/03/30 完成日期 2019/04/05

文章目录


本周概括


学习记录

UIButton

常用 API 简介

  • setTitle:forState: 设置文本

  • setTitleColor:forState: 设置文本颜色

  • setImage:forState: 设置icon(默认左icon右文字,可通过imageEdgeInsets和titleEdgeInsets调整位置)

  • setBackgroundImage:forState: 设置背景图片

  • addTarget:action:forControlEvents: 添加点击事件

  • enabled和userInteractionEnabled 设置按钮是否禁用

  • showsTouchWhenHighlighted 点击按钮显示触摸的特效

用法实例

@interface NetworkAccessViewController() <NSURLSessionDelegate>
@property(nonatomic, strong) IBOutlet UIButton *httpGetButton;
@end
- (void)viewDidLoad {
    [super viewDidLoad];
    self.httpGetButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake(50, 200, 300, 60)];
        [button setTitle:@"发送GET请求查询18世界杯射手榜" forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor blueColor]];
        [button addTarget:self action:@selector(handleGetHttp:) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    [self.view addSubview: self.httpGetButton];
}

网络访问

流程

以发起一个GET请求为例子:

允许IOS9 HTTP请求限制

PIC

用法实例:

以一次 HTTP GET请求为例:

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];

NSURLSession *delegateFreeSession = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:[NSOperationQueue mainQueue]];

NSURL *url = [NSURL URLWithString:@"http://chenbb6.cn:3010/topscorer"];

NSURLSessionDataTask *dataTask = [delegateFreeSession dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if(error == nil) {
        NSString *dataText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"Data = %@", dataText);
    }
    else {
        NSLog(@"%@", error);
    }
}];

[dataTask resume];

工作记录

项目要求

基于自己搭建的简单后台,完成一个网络访问demo


项目结构

PIC


网络服务

实现步骤:

参考博客:

NSJSONSerialization 进行Json解析转化: NSData <----> Foundation

NSDictionary的初始化和一些常用的方法总结

处理本地JSON文件,将data数据转换成NSDictionary

字典(NSDictionary)和JSON字符串(NSString)之间互转

实现代码:

UIButton 按钮的点击响应处理中进行 HTTP GET 网络访问。

- (void)handleGetHttp:(UIButton *)button{
    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];

    NSURLSession *delegateFreeSession = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:[NSOperationQueue mainQueue]];

    NSURL *url = [NSURL URLWithString:@"http://chenbb6.cn:3010/topscorer"];

    NSURLSessionDataTask *dataTask = [delegateFreeSession dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if(error == nil) {
            if(data) {
                NSArray *topScorer = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                TopScorerViewController *controller = [[TopScorerViewController alloc] initWithTopScorerArray:topScorer];
                controller.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:controller animated:YES];
            }
        }
        else {
            NSLog(@"%@", error);
        }
    }];

    [dataTask resume];
}

界面UI

实现效果:

PIC

PIC

主要使用控件:

实现步骤:

关键代码:

- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * cellID = [NSString stringWithFormat:@"cellID:%zd", indexPath.section];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if(nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellID];
    }
    NSDictionary *playerData = self.topScorerList[indexPath.section];
    if(indexPath.row == 0) {
        cell.textLabel.text = [NSString stringWithFormat:@"球员:%@", playerData[@"player_name"]];
    }
    else if(indexPath.row == 1) {
        cell.textLabel.text = [NSString stringWithFormat:@"国家:%@", playerData[@"team_name"]];
    }
    else if(indexPath.row == 2) {
        cell.textLabel.text = [NSString stringWithFormat:@"进球数:%@", playerData[@"goals"]];
    }
    else if(indexPath.row == 3) {
        cell.textLabel.text = [NSString stringWithFormat:@"助攻数:%@", playerData[@"assists"]];
    }
    return cell;
}

总结

标签:indexPath,cell,self,IOS,学习,NSString,button,周报
来源: https://blog.csdn.net/cbb944131226/article/details/89097517