学习制作iOS程序第五天:首页之推荐二手房(18)

十八:首页之四 - 公司推荐的二手房

1、 新建一个RDHeaderView,继承自UIView用于实现通用的header头信息。

#import "RDHeaderView.h"

@implementation RDHeaderView

-(id)initWithFrame:(CGRect)frame andText:(NSString *)strText
{
    if(self=[super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor whiteColor];
        //添加上边框
        CALayer *bottomBorder = [CALayer layer];
        bottomBorder.backgroundColor = BASE_COLOR_BORDERGRAY.CGColor; bottomBorder.frame = CGRectMake(0, self.frame.size.height-0.5f, self.frame.size.width, 0.5f); [self.layer addSublayer:bottomBorder]; //添加上边框 CALayer *topBorder = [CALayer layer]; topBorder.backgroundColor = BASE_COLOR_BORDERGRAY.CGColor; topBorder.frame = CGRectMake(0, 0, self.frame.size.width, 0.5f); [self.layer addSublayer:topBorder]; //添加左侧图片 CALayer *leftBorder = [CALayer layer]; leftBorder.backgroundColor = BASE_COLOR_MAIN.CGColor; leftBorder.frame = CGRectMake(0, 0, 3.5f, self.frame.size.height); [self.layer addSublayer:leftBorder]; //显示文字 UILabel *lbltext = [[UILabel alloc] initWithFrame:CGRectMake(13, 0, self.frame.size.width-13, self.frame.size.height-1.0f)]; lbltext.text = strText; lbltext.textColor = BASE_COLOR_HEADERGRAY; lbltext.font = [UIFont systemFontOfSize:13]; [self addSubview:lbltext]; } return self; } @end

2、创建一个RDHeaderView试试

    //初始化header
    headerview = [[RDHeaderView alloc] initWithFrame:CGRectMake(0, btnbgview.frame.size.height+10, SCREEN_WIDTH, 36.0f) andText:@"推荐二手房"];
    [_scrollview addSubview:headerview];

Header的效果:

3、新建一个Model,HouseListModel,用于记录二手房的实体,具体字段就不做多解释了。

#import <Foundation/Foundation.h>

@interface HouseListModel : NSObject

@property(nonatomic,assign) NSInteger HouseID;
@property(nonatomic,copy) NSString *HouseType;
@property(nonatomic,copy) NSString *HouseTitle;
@property(nonatomic,copy) NSString *HousePrice;
@property(nonatomic,copy) NSString *HouseImage;
@property(nonatomic,copy) NSString *CommInfo;
@property(nonatomic,copy) NSString *HouseInfo;
@property(nonatomic,copy) NSString *Feature1;
@property(nonatomic,copy) NSString *Feature2;
@property(nonatomic,copy) NSString *Feature3;

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

@end
#import "HouseListModel.h"

@implementation HouseListModel

-(instancetype)initWithDict:(NSDictionary *)dict
{
    if (self=[super init]) {
        _HouseID = [[dict objectForKey:@"HouseID"] integerValue];
        _HouseType = [dict objectForKey:@"HouseType"];
        _HousePrice = [dict objectForKey:@"HousePrice"];
        _CommInfo = [dict objectForKey:@"CommInfo"];
        _HouseImage = [dict objectForKey:@"HouseImage"];
        _HouseInfo = [dict objectForKey:@"HouseInfo"];
        _Feature1 = [dict objectForKey:@"Feature1"];
        _Feature2 = [dict objectForKey:@"Feature2"];
        _Feature3 = [dict objectForKey:@"Feature3"];
        _HouseTitle = [dict objectForKey:@"HouseTitle"];
    }
    return self;
}

@end

4、新建一个UITableViewCell,名为HouseListCell,继承自UITableViewCell,用来展示数据

#import <UIKit/UIKit.h>
#import "HouseListModel.h"

@interface HouseListCell : UITableViewCell

@property(nonatomic,strong) UIImageView *HouseImage;
@property(nonatomic,strong) UILabel *HouseTitle;
@property(nonatomic,strong) UILabel *CommInfo;
@property(nonatomic,strong) UILabel *HouseInfo;
@property(nonatomic,strong) UILabel *HousePrice;

@property(nonatomic,strong) UILabel *Feature1;
@property(nonatomic,strong) UILabel *Feature2;
@property(nonatomic,strong) UILabel *Feature3;

@property(nonatomic,strong) HouseListModel *house;

+(instancetype)cellWithTableView:(UITableView *)tableView;

@end
#import "HouseListCell.h"
#import "HouseListModel.h"

@implementation HouseListCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *cellstr = @"houselistcell";
    HouseListCell *cell=[tableView dequeueReusableCellWithIdentifier:cellstr];
    if (cell==nil) {
        cell=[[HouseListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellstr];
    }
    return cell;
}

#pragma mark - 重写初始化方法
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        //_HouseImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 13 ,88, 66)];
        _HouseImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 13 ,96, 71)];
        [self.contentView addSubview:_HouseImage];
        
        //初始化界面现在之创建一个title
        _HouseTitle=[[UILabel alloc]initWithFrame:CGRectMake(116, 8, SCREEN_WIDTH-126, 21)];
        _HouseTitle.font=[UIFont boldSystemFontOfSize:14];
        [self.contentView addSubview:_HouseTitle];
        
        _CommInfo = [[UILabel alloc] initWithFrame:CGRectMake(116, 29, SCREEN_WIDTH-126, 18)];
        _CommInfo.font=[UIFont systemFontOfSize:12];
        _CommInfo.textColor=[UIColor grayColor];
        [self.contentView addSubview:_CommInfo];
        
        _HouseInfo=[[UILabel alloc] initWithFrame:CGRectMake(116, 47, SCREEN_WIDTH-126, 18)];
        _HouseInfo.font=[UIFont systemFontOfSize:12];
        _HouseInfo.textColor=COLOR(50, 50, 50, 1);
        [self.contentView addSubview:_HouseInfo];
        
        _HousePrice=[[UILabel alloc]initWithFrame:CGRectMake(0, 29, SCREEN_WIDTH-10, 25)];
        _HousePrice.textColor=BASE_COLOR_PRICEORANGE;
        _HousePrice.textAlignment=NSTextAlignmentRight;;
        _HousePrice.font=[UIFont systemFontOfSize:15];
        [self.contentView addSubview:_HousePrice];
        
        _Feature1 = [self BuildLabelWithFrame:CGRectMake(116, 68, 40, 17) andColor:COLOR(0, 154, 44, 1) andText:@""];
        [self.contentView addSubview:_Feature1];
        _Feature2 = [self BuildLabelWithFrame:CGRectMake(116, 68, 40, 17) andColor:COLOR(226, 107, 70, 1) andText:@""];
        [self.contentView addSubview:_Feature2];
        _Feature3 = [self BuildLabelWithFrame:CGRectMake(116, 68, 40, 17) andColor:COLOR(15, 111, 172, 1) andText:@""];
        [self.contentView addSubview:_Feature3];
    }
    return self;
}

#pragma mark - 重构函数
-(void)setHouse:(HouseListModel *)modhouse
{
    _house = modhouse;
    
    _HouseTitle.text = modhouse.HouseTitle;
    _CommInfo.text = modhouse.CommInfo;
    _HouseInfo.text = modhouse.HouseInfo;
    if ([modhouse.HouseType isEqualToString:@"1"]) {
        _HousePrice.text=[NSString stringWithFormat:@"%@万",modhouse.HousePrice];
    }else{
        _HousePrice.text=[NSString stringWithFormat:@"%@元",modhouse.HousePrice];
    }
    [_HouseImage sd_setImageWithURL:[NSURL URLWithString:modhouse.HouseImage] placeholderImage:[UIImage imageNamed:@"defaultimage"]];

    //图片展示的时候需要优化显示
    CGFloat maxx=116;
    CGFloat labelwidth = 50;
    CGFloat maxy=68;
    CGFloat labelheight = 16;
    CGFloat labelpadding = 5;
    if ([modhouse.Feature1 isEqualToString:@""]) {
        _Feature1.hidden = YES;
    }else{
        _Feature1.hidden = NO;
        _Feature1.frame=CGRectMake(maxx, maxy, labelwidth, labelheight);
        _Feature1.text = modhouse.Feature1;
        maxx = maxx + labelwidth + labelpadding;
    }
    
    if ([modhouse.Feature2 isEqualToString:@""]) {
        _Feature2.hidden = YES;
    }else{
        _Feature2.hidden = NO;
        _Feature2.frame=CGRectMake(maxx, maxy, labelwidth, labelheight);
        _Feature2.text = modhouse.Feature2;
        maxx = maxx + labelwidth + labelpadding;
    }
    
    if ([modhouse.Feature3 isEqualToString:@""]) {
        _Feature3.hidden = YES;
    }else{
        _Feature3.hidden = NO;
        _Feature3.frame=CGRectMake(maxx, maxy, labelwidth, labelheight);
        _Feature3.text = modhouse.Feature3;
        maxx = maxx + labelwidth + labelpadding;
    }
}

//创建label
-(UILabel *)BuildLabelWithFrame:(CGRect)frame andColor:(UIColor *)lblcolor andText:(NSString *)lbltext
{
    UILabel *lbl = [[UILabel alloc] initWithFrame:frame];
    lbl.font=[UIFont systemFontOfSize:10];
    lbl.textColor = lblcolor;
    lbl.layer.borderColor = lblcolor.CGColor;
    lbl.layer.borderWidth = 0.5f;
    lbl.text=lbltext;
    lbl.textAlignment=NSTextAlignmentCenter;
    
    return lbl;
}
@end

5、主页面添加UITableView信息

(1)引用h文件,定义一些常量

#import "RDMainViewController.h"
#import "RDHeaderView.h"
#import "HouseListCell.h"
#import "HouseListModel.h"
#import "RDSaleDetailViewController.h"

#define MAINBGHEIGHT 200.0f
#define floImageWidth 55.0f
#define floImagePadding (SCREEN_WIDTH - floImageWidth * 4) / 5
#define floRowHeight 98.0f
#define lineMargin UIEdgeInsetsMake(0, 10, 0, 0)

(2)引用代理,定义变量

@interface RDMainViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate, HttpHelperDelegate>
{
    NSArray *arrMenuImage;
    NSArray *arrMenuText;
    UIView *btnbgview;
    RDHeaderView *headerview;
    UITableView *recommandTV;
    NSMutableArray *allhouse;
    HttpHelper *httpHelper;
}
@end

(3)初始化

    //初始化header
    headerview = [[RDHeaderView alloc] initWithFrame:CGRectMake(0, btnbgview.frame.size.height+10, SCREEN_WIDTH, 36.0f) andText:@"推荐二手房"];
    [_scrollview addSubview:headerview];
    
    //初始化请求
    httpHelper = [HttpHelper httpHelper];
    httpHelper.delegate=self;
    [httpHelper GetMainPageRecommandHouseWithHUD:YES];
    
    //初始化uitableview和数据
    allhouse = [NSMutableArray array];
    recommandTV = [[UITableView alloc] initWithFrame:CGRectMake(0, headerview.frame.origin.y + headerview.frame.size.height, SCREEN_WIDTH, 0) style:UITableViewStylePlain];
    recommandTV.scrollEnabled = NO;
    recommandTV.rowHeight = floRowHeight;
    recommandTV.dataSource=self;
    recommandTV.delegate=self;
    if ([recommandTV respondsToSelector:@selector(setSeparatorInset:)]) { [recommandTV setSeparatorInset:lineMargin];}
    if ([recommandTV respondsToSelector:@selector(setLayoutMargins:)]) { [recommandTV setLayoutMargins:lineMargin];}
    
    [_scrollview addSubview:recommandTV];

(4)实现httpHelper的代理函数

#pragma mark - 网络请求回调事件
-(void)HttpExecuteFailure:(AFHTTPRequestOperation *)operation andError:(NSError *)error andFlag:(NSString *)flag
{
    NSLog(@"ERROR=%@",error);
    [self.view makeToast:@"服务器错误,请稍候再试。"];
}
-(void)HttpExecuteSuccess:(AFHTTPRequestOperation *)operation andResponseObject:(id)responseObject andFlag:(NSString *)flag
{
    NSDictionary *dict = (NSDictionary *)responseObject;
    if (1 == [[dict objectForKey:@"success"] integerValue]) {
        [self initDataWithDict:dict];
    }else{
        [self.view makeToast:[dict objectForKey:@"msg"]];
    }
}
-(void)initDataWithDict:(NSDictionary *)dict
{
    [allhouse removeAllObjects];

    NSArray *arrhouse = [dict objectForKey:@"HouseList"];

    for (NSDictionary *dic in arrhouse) {
        HouseListModel *modhouse = [[HouseListModel alloc]initWithDict:dic];
        [allhouse addObject:modhouse];
    }
    
    if (allhouse.count > 0){
        [recommandTV reloadData];
        CGRect tempframe = recommandTV.frame;
        tempframe.size.height = allhouse.count * floRowHeight-1.0f ;
        recommandTV.frame = tempframe;
        [_scrollview setContentSize:CGSizeMake(SCREEN_WIDTH, headerview.frame.origin.y + headerview.frame.size.height + allhouse.count * floRowHeight - 1.0f)];
    }else{
        CGRect tempframe = recommandTV.frame;
        tempframe.size.height = 0;
        recommandTV.frame = tempframe;
        [_scrollview setContentSize:CGSizeMake(SCREEN_WIDTH, headerview.frame.origin.y + headerview.frame.size.height)];
    }
}

(5)实现tableview的代理函数

#pragma mark - uitableview代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    //点击跳到详情页
    HouseListModel *modhouse = allhouse[indexPath.row];
    RDSaleDetailViewController *detail = [[RDSaleDetailViewController alloc]init];
    detail.HouseID=modhouse.HouseID;
    detail.HouseType=modhouse.HouseType;
    self.navigationItem.backBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationController.navigationBar.hidden=NO;
    [self.navigationController pushViewController:detail animated:NO];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return allhouse.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HouseListCell *cell=[HouseListCell cellWithTableView:tableView];
    HouseListModel *modhouse = allhouse[indexPath.row];
    cell.house=modhouse;
    return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:lineMargin];}
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:lineMargin];}
}
@end

(6)因为httphelper调用了方法,需要在httphelper.h和httphelper.m中新增对应的方法。

#pragma mark - 获取推荐房源
-(void)GetMainPageRecommandHouseWithHUD:(BOOL)ishud;
#pragma mark - 获取首页推荐房源
-(void)GetMainPageRecommandHouseWithHUD:(BOOL)ishud
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [self postDataWithURLString:[NSString stringWithFormat:@"%@%@",BASE_URL,BASE_URL_MAIN_RECOMMAND] andParameters:dict andHUD:ishud andFlag:@"Main_GetRecommand"];
}

(7)最终的效果。

 

 

知识一:怎么修改每个cell的分隔线和左边的距离?

在ios7中,UITableViewCell左侧会有默认15像素的空白。这时候,设置setSeparatorInset:UIEdgeInsetsZero 能将空白去掉。
但是在ios8中,设置setSeparatorInset:UIEdgeInsetsZero 已经不起作用了。下面是解决办法
首先在viewDidLoad方法加入以下代码:

if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
    [self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
    [self.tableView setLayoutMargins:UIEdgeInsetsZero];
}

然后在UITableView的代理方法中加入以下代码

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}        

 

知识二:为什么我的cell的分隔线会闪烁?

我在测试的时候发现有时候分隔线会闪烁,有时候粗,有时候细。原因是没有为6和6p做适配,调用的是5的适配,所以会发生这种问题。

做下6和6p的适配即可。

 

转载于:https://www.cnblogs.com/randytech/p/5037977.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值