问题
问题
StartTime 发表于11个月前
问题
  • 发表于 11个月前
  • 阅读 15
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

//

//  SellProdViewController.m

//  souketong

//

//  Created by chang on 16/8/7.

//  Copyright © 2016年 souketong. All rights reserved.

//

 

#import "SellProdViewController.h"

#import "MBProgressHUD.h"

#import "MJRefresh.h"

@interface SellProdViewController ()

@property(nonatomic,strong) NSDictionary *userJsonDic;

@property(nonatomic,strong) NSMutableArray *listData;

@property(strong, nonatomic) MBProgressHUD *hud;

//@property(strong, nonatomic) UITableView *_tableView;

@property (assign, nonatomic) NSInteger page; //!< 数据页数.表示下次请求第几页的数据.

@end

@implementation SellProdViewController

 

-(void)setSingleLineTitle:(NSString *)title{

    //定义导航栏文字

    UILabel *titlelable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 44)];

    titlelable.font = [UIFont boldSystemFontOfSize:17];

    titlelable.textAlignment = NSTextAlignmentCenter;

    titlelable.text = title;

    titlelable.textColor = [UIColor whiteColor];

    titlelable.backgroundColor = [UIColor clearColor];

    self.navigationItem.titleView = titlelable;

}

 

-(void)addSellContent{

}

 

- (void)viewDidLoad {

    

    [super viewDidLoad];

    

    self.listData = [[NSMutableArray alloc] init];

    

    self.view.backgroundColor = [UIColor whiteColor];

    

    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.2 green:0.72 blue:0.46 alpha:0.6]];

    

    [self setSingleLineTitle:@"业务市场"];

    //添加按钮

    UIBarButtonItem *post = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addSellContent)];

    

    post.tintColor = [UIColor whiteColor];

    

    self.navigationItem.rightBarButtonItem = post;

    

    //进度提示

    

    self.hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];

    

    self.hud.labelFont = [UIFont italicSystemFontOfSize:16.f];

    

    self.hud.labelText = @"正在加载数据";

 

    NSLog(@"正在加载数据");

    

    [self loadDataList];

    

    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

    

    self.tableView.delegate = self;

    

    self.tableView.dataSource = self;

 

    [self Refresh];

    

    //[self.view addSubview:self.tableView];

}

 

- (void)Refresh

{

    

    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadDataList)];

    [self.tableView.mj_footer beginRefreshing];

    // 设置文字

    [footer setTitle:@"向上拉动屏幕加载更多..." forState:MJRefreshStateIdle];

    [footer setTitle:@"正在加载 ..." forState:MJRefreshStateRefreshing];

    [footer setTitle:@"没有数据了..." forState:MJRefreshStateNoMoreData];

    // 设置字体

    footer.stateLabel.font = [UIFont systemFontOfSize:17];

    // 设置footer

    self.tableView.mj_footer = footer;

}

 

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    [self.tableView.mj_footer beginRefreshing];

 

}

 

- (void)loadDataList{

    //self.listData = [NSMutableArray arrayWithCapacity:0];

 

    //请求服务器

    

    NSURL *loginUrl = [NSURL URLWithString:@"http://api.souketong.com/index.php?c=sell&a=sell_list"];

    

    NSInteger userId = 244;

    

    NSString *screat = @"ef50fAFAEXQtaCwZTCQBdWgZXVwUEUlpWUVdTAlsKBFE";

    

    NSString *str = [NSString stringWithFormat:@"userId=%ld&screat=%@&p=%ld",userId,screat,self.page++];

    

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:loginUrl];

    

    request.HTTPMethod = @"POST";     //请求方式POST

    

    request.HTTPBody = [str dataUsingEncoding:NSUTF8StringEncoding]; //编码

    

    NSURLSession *session = [NSURLSession sharedSession]; //构造SESSION

    

    NSLog(@"userId=%ld&screat=%@&p=%ld",userId,screat,self.page);

    

    NSURLSessionDataTask *task =  [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        

        NSString *sellJsonString =  [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

        //解析返回的JSON数据

        self.userJsonDic = [NSJSONSerialization JSONObjectWithData:[sellJsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];

        self.listData =  [self.userJsonDic objectForKey:@"result"];

        [self.tableView reloadData];

       

        if(self.listData.count<10){

            [self.tableView.mj_footer endRefreshing];

        }

        

        [self.tableView.mj_footer endRefreshing];

        

 

    }];

    

    [self.hud hide:YES];

    

    [task resume];   //执行task

    

 

}

 

 

 

//加载完成后刷新表格

/*

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"TEST");

    if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){

        //end of loading

        //for example [activityIndicator stopAnimating];

        

        NSLog(@"TEST");

        

        [tableView reloadData];

        

    }

    

}

*/

//表格行数 必须要实现的

//返回值为每组元素的个数

//P1: 视图对象本身

//P2: 哪一组需要的行数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

     return self.listData.count;

}

 

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}

 

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    // 定义cell标识  每个cell对应一个自己的标识

    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section,indexPath.row];

    // 通过不同标识创建cell实例

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,而不会再次初始化)

    

    tableView.rowHeight = 80;

    

    if(cell==nil){

        

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        

    }

    

    NSMutableDictionary * dict = self.listData[indexPath.row];

    

    cell.textLabel.text = [dict objectForKey:@"sellName"];

    

    cell.detailTextLabel.text = [dict objectForKey:@"CallingName"];

    

    cell.detailTextLabel.textColor = [UIColor colorWithRed:60/255.0 green:60/255.0 blue:60/255.0 alpha:0.5];

    

    cell.textLabel.font = [UIFont fontWithName:@"Party LET" size:14];

    

    NSURL *imageurl = [NSURL URLWithString:[dict objectForKey:@"userIcon"]];

    

    NSData *urlImageData = [NSData dataWithContentsOfURL:imageurl];

    

    CGSize size={50,50};

    

    UIImage *icon  = [self imageCompressWithSimple:[UIImage imageWithData:urlImageData] scaledToSize:size];

    

    cell.imageView.image= icon;

 

    return cell;

}

 

//图片缩放

- (UIImage*)imageCompressWithSimple:(UIImage*)image scaledToSize:(CGSize)size

{

    UIGraphicsBeginImageContext(size);

    [image drawInRect:CGRectMake(0,0,size.width,size.height)];

    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;

}

 

//点表格后的跳转

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

    

    NSLog(@"slecteddsddd %d",(int)indexPath.row);

    

    

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

/*

#pragma mark - Navigation

 

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

 

@end

 

共有 人打赏支持
粉丝 3
博文 2
码字总数 1546
×
StartTime
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: