UITableView_5-团购简单实现,nib封装实现,模型封装实现


整体文件的结构

ViewController.m文件==============================

//  TG团购

//

//  Created by Toge on 6/5/15.

//  Copyright (c) 2015 wxhl. All rights reserved.

//


#import "ViewController.h"

#import "TGtg.h"

#import "TGtgCell.h"


@interface ViewController ()<UITableViewDataSource>

@property (weak, nonatomic) IBOutletUITableView *tableView;


@end


@implementation ViewController

- (NSArray *)tgs

{

    if (_tgs ==nil) {

        

//        UINib *nib =  [UINib nibWithNibName:@"tgs" bundle:nil];

//        NSArray *array = [nib instantiateWithOwner:nil options:nil];

        NSString *path = [[NSBundlemainBundle]pathForResource:@"tgs"ofType:@"plist"];

        NSArray *array = [NSArrayarrayWithContentsOfFile:path];

        

        NSMutableArray *tempArray = [[NSMutableArrayalloc]initWithCapacity:15];

        for (NSDictionary *dict in array) {

            TGtg *tg = [TGtgtgWithDict:dict];

            

            [tempArray addObject:tg];

        }

        

        _tgs = tempArray;

        

    }

    

    return _tgs;

}


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    self.tableView.rowHeight =80;

}


//先把电池栏隐藏

- (UIStatusBarStyle)preferredStatusBarStyle

{

    return  YES;

}

#pragma mark - datasource

//加载多少组数据 ,此处只有一组可以省略

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

//{

//    

//}

//加载多少数据

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

{

    return self.tgs.count;

}


//创建cell

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

{

    //调用封装的类,返回需要的cell,控制器并不需要知道,cell中有那些数据

    TGtgCell *cell = [TGtgCelltgWithTableView:tableView];

    //获得对应的模型

    TGtg *tg = self.tgs[indexPath.row];

    //将活的的模型传给,封装类,实现的过程控制器并不需要知道

    cell.tg = tg;

    

    return cell;

}


@end


TGtgCell.m=======================================

//  TG团购

//

//  Created by Toge on 6/5/15.

//  Copyright (c) 2015 wxhl. All rights reserved.

//


#import "TGtgCell.h"

#import "TGtg.h"

@interface TGtgCell ()

@property (weak, nonatomic) IBOutletUIImageView *iconView;

@property (weak, nonatomic) IBOutletUILabel *titleLabel;

@property (weak, nonatomic) IBOutletUILabel *priceLabel;

@property (weak, nonatomic) IBOutletUILabel *buyCountLabel;



@end


@implementation TGtgCell

//封装nib文件的加载,直接返回cell

+ (instancetype)tgWithTableView:(UITableView *)tableView

{

    //定义标识符

    static NSString *identify =@"tg";

    //在内存中找对应的cell

    TGtgCell *cell = [tableViewdequeueReusableHeaderFooterViewWithIdentifier:identify];

    //判断cell是否存在,不存在就重新创建

    if (cell == nil) {

        //        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identify];

        cell = [[[NSBundlemainBundleloadNibNamed:@"TGtgcell"owner:niloptions:nil]lastObject];

    }

    

    return cell;

}

//将此tableviwsubview的赋值进行封装封装,将模型传进来

- (void)setTg:(TGtg *)tg

{

    _tg = tg;       //这不一定要有

    self.iconView.image = [UIImageimageNamed:tg.icon];

    self.titleLabel.text = tg.title;

    self.priceLabel.text = [NSStringstringWithFormat:@"%@",tg.price];

    self.buyCountLabel.text = [NSStringstringWithFormat:@"%@人购买",tg.buyCount];

}

@end


//  TGtgCell.h========================================

//  TG团购

//

//  Created by Toge on 6/5/15.

//  Copyright (c) 2015 wxhl. All rights reserved.

//


#import <UIKit/UIKit.h>

@class TGtg;

@interface TGtgCell :UITableViewCell


//封装读取nib文件

+ (instancetype)tgWithTableView:(UITableView *)tableview;


//将模型传给view初始化此view中的各subview

@property (nonatomic,strong)TGtg *tg;


@end




//  TGtg.m===========================================

//  TG团购

//

//  Created by Toge on 6/5/15.

//  Copyright (c) 2015 wxhl. All rights reserved.

//


#import "TGtg.h"


@implementation TGtg


- (instancetype)initWithDict:(NSDictionary *)dict

{

    if (self = [superinit]) {

        //KVC方式给模型赋值

        [selfsetValuesForKeysWithDictionary:dict];

    }

    return  self;

}

+ (instancetype)tgWithDict:(NSDictionary *)dict

{

    return [[selfalloc]initWithDict:dict];

}


@end


//  TGtg.h=================================

//  TG团购

//

//  Created by Toge on 6/5/15.

//  Copyright (c) 2015 wxhl. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface TGtg : NSObject


//模型的属性,和对应字典中的key名字相同

@property (nonatomic,copy)NSString *title;

@property (nonatomic,copy)NSString *price;

@property (nonatomic,copy)NSString *icon;

@property (nonatomic,copyNSString *buyCount;


- (instancetype)initWithDict:(NSDictionary *)dict;

+ (instancetype)tgWithDict:(NSDictionary *)dict;


@end

最后效果图



先展示下效果 https://pan.quark.cn/s/5061241daffd 在使用Apache HttpClient库发起HTTP请求的过程中,有可能遇到`HttpClient`返回`response`为`null`的现象,这通常暗示着请求未能成功执行或部分资源未能得到妥善处理。 在本文中,我们将详细研究该问题的成因以及应对策略。 我们需要掌握`HttpClient`的运作机制。 `HttpClient`是一个功能强大的Java库,用于发送HTTP请求并接收响应。 它提供了丰富的API,能够处理多种HTTP方法(例如GET、POST等),支持重试机制、连接池管理以及自定义请求头等特性。 然而,一旦`response`对象为`null`,可能涉及以下几种情形:1. **连接故障**:网络连接未成功建立或在请求期间中断。 需要检查网络配置,确保服务器地址准确且可访问。 2. **超时配置**:若请求超时,`HttpClient`可能不会返回`response`。 应检查连接和读取超时设置,并根据实际需求进行适当调整。 3. **服务器故障**:服务器可能返回了错误状态码(如500内部服务器错误),`HttpClient`无法解析该响应。 建议查看服务器日志以获取更多详细信息。 4. **资源管理**:在某些情况下,如果请求的响应实体未被正确关闭,可能导致连接被提前释放,进而使后续的`response`对象为`null`。 在使用`HttpClient 3.x`版本时,必须手动调用`HttpMethod.releaseConnection()`来释放连接。 而在`HttpClient 4.x`及以上版本中,推荐采用`EntityUtils.consumeQuietly(respons...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值