UITableView cell自定义头文件:

shopCell.h

#import <UIKit/UIKit.h>

@interface shopCell : UITableViewCell

@property (strong, nonatomic)  UIImageView *image;
@property (strong, nonatomic)  UILabel *name;
@property (strong, nonatomic)  UILabel *itemshop;
@property (strong, nonatomic)  UILabel *itemshopprice;
@property (strong, nonatomic)  UIButton *focus;
@property (strong, nonatomic)  UIButton *share;
@property (strong, nonatomic)  UIView *shopAreaView;
@property (strong, nonatomic)  UIView *goodsAreaView;
@property (strong, nonatomic)  UIView *lineView;
@property (strong, nonatomic)  UIView *searchAreaView;

@end

对应的.m文件
#import "shopCell.h"
#import "FTMacro.h"
@implementation shopCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        int shopAreaViewHeight=1;
        int goodsAreaViewHeight=1;
        int goodsAreaViewLeft=kGoodsAreaViewGap;
        _image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 120, 120)];
        [self.contentView addSubview:_image];
        
        
        
        _name = [[UILabel alloc] initWithFrame:CGRectMake(145, 18, kScreenWidth-155, 90)];
        _name.numberOfLines = 0;
        _name.font = [UIFont systemFontOfSize: 15.0];
        [self.contentView addSubview:_name];
        
        _itemshop = [[UILabel alloc] initWithFrame:CGRectMake(145, 100,65, 20)];
        _itemshop.numberOfLines = 1;
        _itemshop.textAlignment=NSTextAlignmentRight;
        _itemshop.font = [UIFont systemFontOfSize: 13.0];
        [self.contentView addSubview:_itemshop];
        
        _itemshopprice = [[UILabel alloc] initWithFrame:CGRectMake((145+65), 100, 60, 20)];
        _itemshopprice.textAlignment=NSTextAlignmentLeft;
        _itemshopprice.numberOfLines = 1;
        _itemshopprice.textColor= [UIColor redColor];
        _itemshopprice.font = [UIFont systemFontOfSize: 13.0];
        [self.contentView addSubview:_itemshopprice];
        
        _searchAreaView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth-135, 15, 135,20)];
        [self.contentView addSubview:_searchAreaView];
        
        _focus = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth-70, 87, 40, 40)];
        [self.contentView addSubview:_focus];
        
        _share = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth-40, 89, 40, 40)];
        [self.contentView addSubview:_share];
        
        _shopAreaView = [[UIView alloc] initWithFrame:CGRectMake(0, 140, kScreenWidth,shopAreaViewHeight)];
        [self.contentView addSubview:_shopAreaView];
        
        _goodsAreaView = [[UIView alloc] initWithFrame:CGRectMake(goodsAreaViewLeft, 140+shopAreaViewHeight, kScreenWidth-2*goodsAreaViewLeft,goodsAreaViewHeight)];
        [self.contentView addSubview:_goodsAreaView];
        
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(5, 140+shopAreaViewHeight + goodsAreaViewHeight+13, kScreenWidth - 10, 1)];
        _lineView.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0.85 alpha:1];
        [self.contentView addSubview:_lineView];
    }
    return self;
}

@end

UITableView cell自定义头文件:shopCell.h#import <UIKit/UIKit.h>@interface shopCell : UITableViewCell@property (strong, nonatomic)  UIImageView *image;
@property (strong, nonatomic)  UILabel *name;
@property (strong, nonatomic)  UILabel *itemshop;
@property (strong, nonatomic)  UILabel *itemshopprice;
@property (strong, nonatomic)  UIButton *focus;
@property (strong, nonatomic)  UIButton *share;
@property (strong, nonatomic)  UIView *shopAreaView;
@property (strong, nonatomic)  UIView *goodsAreaView;
@property (strong, nonatomic)  UIView *lineView;
@property (strong, nonatomic)  UIView *searchAreaView;@end对应的.m文件
#import "shopCell.h"
#import "FTMacro.h"
@implementation shopCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];if (self) {int shopAreaViewHeight=1;int goodsAreaViewHeight=1;int goodsAreaViewLeft=kGoodsAreaViewGap;_image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 120, 120)];[self.contentView addSubview:_image];_name = [[UILabel alloc] initWithFrame:CGRectMake(145, 18, kScreenWidth-155, 90)];_name.numberOfLines = 0;_name.font = [UIFont systemFontOfSize: 15.0];[self.contentView addSubview:_name];_itemshop = [[UILabel alloc] initWithFrame:CGRectMake(145, 100,65, 20)];_itemshop.numberOfLines = 1;_itemshop.textAlignment=NSTextAlignmentRight;_itemshop.font = [UIFont systemFontOfSize: 13.0];[self.contentView addSubview:_itemshop];_itemshopprice = [[UILabel alloc] initWithFrame:CGRectMake((145+65), 100, 60, 20)];_itemshopprice.textAlignment=NSTextAlignmentLeft;_itemshopprice.numberOfLines = 1;_itemshopprice.textColor= [UIColor redColor];_itemshopprice.font = [UIFont systemFontOfSize: 13.0];[self.contentView addSubview:_itemshopprice];_searchAreaView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth-135, 15, 135,20)];[self.contentView addSubview:_searchAreaView];_focus = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth-70, 87, 40, 40)];[self.contentView addSubview:_focus];_share = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth-40, 89, 40, 40)];[self.contentView addSubview:_share];_shopAreaView = [[UIView alloc] initWithFrame:CGRectMake(0, 140, kScreenWidth,shopAreaViewHeight)];[self.contentView addSubview:_shopAreaView];_goodsAreaView = [[UIView alloc] initWithFrame:CGRectMake(goodsAreaViewLeft, 140+shopAreaViewHeight, kScreenWidth-2*goodsAreaViewLeft,goodsAreaViewHeight)];[self.contentView addSubview:_goodsAreaView];_lineView = [[UIView alloc] initWithFrame:CGRectMake(5, 140+shopAreaViewHeight + goodsAreaViewHeight+13, kScreenWidth - 10, 1)];_lineView.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0.85 alpha:1];[self.contentView addSubview:_lineView];}return self;
}@end在UITableView 中使用:HomeViewController.h 代码#ifndef HomeViewController_h@interface HomeViewController : UIViewController@property (nonatomic, strong) NSMutableArray *tableArray;
@property (strong, nonatomic) UITableView *mTableView;
@end
#endifHomeViewController.m  部分代码- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    NSLog(@"numberOfSectionsInTableView");return 1;
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    NSLog(@"numberOfRowsInSection:%d",self.tableArray.count);return self.tableArray.count;
}下面的函数计算cell的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{//    NSLog(@"heightForRowAtIndexPath-- height=%f",height);int shopAreaHeight=10;int goodsAreaHeight=10;itemModel *model = [self.tableArray objectAtIndex:indexPath.row];double itemshopprice=0;int rowindex=0,colindex=0;NSArray *arr = model.goodsArray;NSArray *newArr = model.ebGoodsArray;//NSLog(@"_kShopItemCols:%d",_kShopItemCols);int tmpebid=0;int ebidnum=0;BOOL moreItemDisplayed=false;if(arr){//NSLog(@"type4id:%d",model.type4id);int t1id=[_appDelegate getType1idByType4id:model.type4id];//NSLog(@"t1id:%d",t1id);NSMutableArray *ebidarrAll=[_appDelegate geEBArrByType1id:t1id];int ebidarrAllcount=ebidarrAll.count;//NSLog(@"ebidarrAllcount:%d",ebidarrAllcount);for(int m=0;m<ebidarrAllcount;m++){int arrebid=[ebidarrAll[m] intValue];if(model.ebid==arrebid){[ebidarrAll removeObjectAtIndex:m];break;}}//NSLog(@"rr arr.count:%d",arr.count);for(int i=0;i<arr.count;i++){goodsModel *goods = [arr objectAtIndex:i];if(goods && goods.goodsid>0){double price=goods.price / 100;NSString *shopname=[_appDelegate getShopNameByEBID:goods.ebid];if(model.ebid==goods.ebid){itemshopprice=price;}if(tmpebid!=goods.ebid && goods.ebid!=model.ebid){tmpebid=goods.ebid;rowindex=floor((double)ebidnum / _kShopItemCols);colindex=ebidnum % _kShopItemCols;// NSLog(@"rr rowindex:%d,colindex:%d",rowindex,colindex);ebidnum++;// 为了找出不存在的商家ebidarrAllcount=ebidarrAll.count;for(int m=0;m<ebidarrAllcount;m++){int arrebid=[ebidarrAll[m] intValue];if(goods.ebid==arrebid || model.ebid==arrebid){[ebidarrAll removeObjectAtIndex:m];break;}}}}else{//                NSLog(@"name:%@,price=%d",model.name,goods.price);}}// 显示不存在的商品的商家ebidarrAllcount=ebidarrAll.count;//NSLog(@"ebidarr not existed count:%d",ebidarrAllcount);for(int m=0;m<ebidarrAllcount;m++){int tmpebid=[ebidarrAll[m] intValue];rowindex=floor((double)ebidnum / _kShopItemCols);colindex=ebidnum % _kShopItemCols;ebidnum++;}}//NSLog(@"rr kItemcolheight:%d,rowindex:%d",kItemcolheight,rowindex);shopAreaHeight = kItemcolheight * (rowindex+1);//NSLog(@"rr shopAreaHeight:%d,rowindex:%d",shopAreaHeight,rowindex);if(newArr){goodsAreaHeight = 0;tmpebid=0;int oneebidgoodsnum=0;rowindex=0;int oneebGoodsAreaHeight=0;int moreItemHeight=kItemcolheight*1.5;for(int i=0;i<newArr.count;i++){goodsModel *goods = [newArr objectAtIndex:i];// NSLog(@"show goods newArr i:%d",i);if(goods && goods.display && model.ebid!=goods.ebid && goods.goodsid>0){// double price=goods.price / 100;// NSString *shopname=[_appDelegate getShopNameByEBID:goods.ebid];if(tmpebid!=goods.ebid){goodsAreaHeight+=oneebGoodsAreaHeight;oneebGoodsAreaHeight=0;rowindex=0;oneebidgoodsnum=0;moreItemDisplayed=false;// NSLog(@"show goods shopname i:%@",shopname);tmpebid=goods.ebid;// 显示商家名称goodsAreaHeight+=kItemcolheight*2;//NSLog(@"rr 1 shopAreaHeight:%d,goodsAreaHeight:%d",shopAreaHeight,goodsAreaHeight);}rowindex=floor((double)oneebidgoodsnum / _kShopGoodsCols);if(rowindex > displayRowIndexMax){if(goods.displayMore){if(!moreItemDisplayed){// 显示“收起”moreItemDisplayed=true;}}else{if(!moreItemDisplayed){// 显示“更多”moreItemDisplayed=true;oneebGoodsAreaHeight=moreItemHeight+(rowindex)*kOneGoodsHight;}continue;}}{rowindex=floor((double)oneebidgoodsnum / _kShopGoodsCols);oneebGoodsAreaHeight=(rowindex+1)*kOneGoodsHight;if(rowindex > displayRowIndexMax){oneebGoodsAreaHeight=moreItemHeight+(rowindex+1)*kOneGoodsHight;}oneebidgoodsnum++;//NSLog(@"rr oneebidgoodsnum:%d",oneebidgoodsnum);}}else{//                NSLog(@"name:%@,price=%d",model.name,goods.price);}}goodsAreaHeight+=oneebGoodsAreaHeight;}else{//        NSLog(@"name:%@,goodsarr=nil",model.name);}int newHeight=shopAreaHeight+goodsAreaHeight;//NSLog(@"rr newHeight:%d,shopAreaHeight:%d,goodsAreaHeight:%d",newHeight,shopAreaHeight,goodsAreaHeight);int height=kItemstartypos+newHeight+1;//NSLog(@"rr heightForRowAtIndexPath-- height:%d",height);return height;
}下面的函数实现tableview对应的行- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{// NSLog(@"-------cellForRowAtIndexPath:%d ---------",indexPath.row);itemModel *model = [self.tableArray objectAtIndex:indexPath.row];if(model.itemid!=0){NSString * dentifier =  [NSString stringWithFormat:@"itemcell_%d",indexPath.row];shopCell *cell = [tableView dequeueReusableCellWithIdentifier:dentifier];if(cell == nil){//        NSLog(@"cell nil!");cell = [[shopCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: dentifier];}else{//NSLog(@"cell exist!");// return cell;for(UIView *view in [cell.searchAreaView subviews]){[view removeFromSuperview];}for(UIView *view in [cell.shopAreaView subviews]){[view removeFromSuperview];}}cell.selectionStyle =  UITableViewCellSelectionStyleNone;[cell.image sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:[UIImage imageNamed:@"yxshpic_default"]];if(_appDelegate.share == 1){[cell.share setImage:[UIImage imageNamed:@"fx_n.png"]forState:UIControlStateNormal];[cell.share setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];cell.share.backgroundColor = [UIColor clearColor];[cell.share setTitleColor:[UIColor redColor]forState:UIControlStateNormal];cell.share.titleLabel.font = [UIFont systemFontOfSize: kTextFontsize1];[cell.share addTarget:self action:@selector(onClicksShare:) forControlEvents:UIControlEventTouchUpInside];[cell.share setTag:(indexPath.row)];}cell.name.text = [NSString stringWithFormat:@"%@",model.name];cell = [self configureCell:cell atIndexPath:indexPath];return cell;}else{NSString * dentifier =  [NSString stringWithFormat: @"aditemcell%d",indexPath.row];adCell *cell = [tableView dequeueReusableCellWithIdentifier:dentifier];if(cell == nil){//        NSLog(@"ad cell nil!");cell = [[adCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: dentifier];}else{// NSLog(@"ad cell exist!");// return cell;}cell.selectionStyle =  UITableViewCellSelectionStyleNone;[cell.image sd_setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:[UIImage imageNamed:model.pic]];return cell;}
}下面的函数动态填充cell里的各元素并返回cell- (shopCell *)configureCell:(shopCell *)cell atIndexPath:(NSIndexPath *)indexPath
{// NSLog(@"config:%d",indexPath.row);for(UIView *view in [cell.shopAreaView subviews]){[view removeFromSuperview];}for(UIView *view in [cell.goodsAreaView subviews]){[view removeFromSuperview];}int shopAreaHeight=10;int goodsAreaHeight=10;itemModel *model = [self.tableArray objectAtIndex:indexPath.row];double itemshopprice=0;NSString *itemshopname=[_appDelegate getShopNameByEBID:model.ebid];double price0=0.0,pricelast=0.0;int rowindex=0,colindex=0;NSArray *arr = model.goodsArray;NSArray *newArr = model.ebGoodsArray;int tmpebid=0;int ebidnum=0;if(arr){//NSLog(@"type4id:%d",model.type4id);int t1id=[_appDelegate getType1idByType4id:model.type4id];//NSLog(@"t1id:%d",t1id);NSMutableArray *ebidarrAll=[_appDelegate geEBArrByType1id:t1id];int ebidarrAllcount=ebidarrAll.count;//NSLog(@"ebidarrAllcount:%d",ebidarrAllcount);for(int m=0;m<ebidarrAllcount;m++){int arrebid=[ebidarrAll[m] intValue];if(model.ebid==arrebid){[ebidarrAll removeObjectAtIndex:m];break;}}// NSLog(@"arr.count:%d",arr.count);for(int i=0;i<arr.count;i++){goodsModel *goods = [arr objectAtIndex:i];if(goods && goods.goodsid>0){double price=goods.price / 100;NSString *shopname=[_appDelegate getShopNameByEBID:goods.ebid];if(model.ebid==goods.ebid){itemshopprice=price;}// 显示商家列表if(tmpebid!=goods.ebid && goods.ebid!=model.ebid){tmpebid=goods.ebid;rowindex=floor((double)ebidnum / _kShopItemCols);colindex=ebidnum % _kShopItemCols;//  NSLog(@"lowindex:%d,colindex:%d",lowindex,colindex);int posx=_kShopItemWidth*colindex;int posy=kItemcolheight*rowindex;int labelwidth=_kShopItemWidth;int labelheight=kItemcolheight;//  NSLog(@"label1:%d,%d,%d,%d",posx,posy,labelwidth,labelheight);UIButton *shoplabel = [[UIButton alloc] initWithFrame:CGRectMake(posx,posy,labelwidth,labelheight)];[shoplabel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];[shoplabel setTitle:[NSString stringWithFormat:@"%@",shopname] forState: UIControlStateNormal];shoplabel.backgroundColor = [UIColor clearColor];if(goods.display){[shoplabel setTitleColor:[UIColor redColor]forState:UIControlStateNormal];}else{[shoplabel setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];}shoplabel.titleLabel.font = [UIFont systemFontOfSize: kTextFontsize1];[shoplabel addTarget:self action:@selector(shopItemClick:) forControlEvents:UIControlEventTouchUpInside];[shoplabel setTag:(indexPath.row*100+goods.ebid)];[cell.shopAreaView addSubview:shoplabel];ebidnum++;// 为了找出不存在的商家ebidarrAllcount=ebidarrAll.count;for(int m=0;m<ebidarrAllcount;m++){int arrebid=[ebidarrAll[m] intValue];if(goods.ebid==arrebid || model.ebid==arrebid){[ebidarrAll removeObjectAtIndex:m];break;}}}}else{
//                NSLog(@"name:%@,price=%d",model.name,goods.price);}}// 显示不存在的商品的商家ebidarrAllcount=ebidarrAll.count;//NSLog(@"ebidarr not existed count:%d",ebidarrAllcount);for(int m=0;m<ebidarrAllcount;m++){int tmpebid=[ebidarrAll[m] intValue];NSString *shopname=[_appDelegate getShopNameByEBID:tmpebid];rowindex=floor((double)ebidnum / _kShopItemCols);colindex=ebidnum % _kShopItemCols;//  NSLog(@"lowindex:%d,colindex:%d",lowindex,colindex);/*int posx=_kShopItemWidth*colindex;int posy=kItemcolheight*rowindex+2;int labelwidth=15;int labelheight=15;//  NSLog(@"label1:%d,%d,%d,%d",posx,posy,labelwidth,labelheight);// 显示搜索图标UIButton *websearchlabel = [[UIButton alloc] initWithFrame:CGRectMake(posx,posy,labelwidth,labelheight)];[websearchlabel setImage:[UIImage imageNamed:@"search_k.png"]forState:UIControlStateNormal];[websearchlabel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];websearchlabel.backgroundColor = [UIColor clearColor];[websearchlabel setTitleColor:[UIColor redColor]forState:UIControlStateNormal];websearchlabel.titleLabel.font = [UIFont systemFontOfSize: kTextFontsize1];[websearchlabel addTarget:self action:@selector(shopSearchItemClick:) forControlEvents:UIControlEventTouchUpInside];[websearchlabel setTag:(indexPath.row*100+tmpebid)];[cell.shopAreaView  addSubview:websearchlabel];*/// 显示商家名称int posx=_kShopItemWidth*colindex;int posy=kItemcolheight*rowindex;int labelwidth=_kShopItemWidth;int labelheight=kItemcolheight;UIButton *shoplabel = [[UIButton alloc] initWithFrame:CGRectMake(posx,posy,labelwidth,labelheight)];[shoplabel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];[shoplabel setTitle:[NSString stringWithFormat:@"%@",shopname] forState: UIControlStateNormal];shoplabel.backgroundColor = [UIColor clearColor];[shoplabel setTitleColor:self.view.tintColor forState:UIControlStateNormal];shoplabel.titleLabel.font = [UIFont systemFontOfSize: kTextFontsize1];[shoplabel addTarget:self action:@selector(shopSearchItemClick:) forControlEvents:UIControlEventTouchUpInside];[shoplabel setTag:(indexPath.row*100+tmpebid)];[cell.shopAreaView addSubview:shoplabel];ebidnum++;}}shopAreaHeight = kItemcolheight * (rowindex+1);//NSLog(@"shopAreaHeight=%d,rowindex=%d",shopAreaHeight,rowindex);if(newArr){goodsAreaHeight = 0;tmpebid=0;int oneebidgoodsnum=0;rowindex=0;int oneebGoodsAreaHeight=0;BOOL moreItemDisplayed=false;int moreItemHeight=kItemcolheight*1.5;for(int i=0;i<newArr.count;i++){goodsModel *goods = [newArr objectAtIndex:i];// NSLog(@"show goods newArr i:%d",i);if(goods && goods.display && model.ebid!=goods.ebid && goods.goodsid>0){double price=goods.price / 100;NSString *shopname=[_appDelegate getShopNameByEBID:goods.ebid];if(tmpebid!=goods.ebid){goodsAreaHeight+=oneebGoodsAreaHeight;oneebGoodsAreaHeight=0;rowindex=0;oneebidgoodsnum=0;moreItemDisplayed=false;//NSLog(@"show goods shopname i:%@",shopname);tmpebid=goods.ebid;// 显示商家名称int posx=0;int posy=goodsAreaHeight;int labelwidth=150;int labelheight=kItemcolheight;//NSLog(@"显示商家名称 shopAreaHeight:%d,goodsAreaHeight:%d,posy:%d,labelheight:%d",shopAreaHeight,goodsAreaHeight,posy,labelheight);UIButton *shoplabel = [[UIButton alloc] initWithFrame:CGRectMake(posx,posy,labelwidth,labelheight)];[shoplabel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];[shoplabel setTitle:[NSString stringWithFormat:@"%@相关商品 | 隐藏",shopname] forState: UIControlStateNormal];shoplabel.backgroundColor = [UIColor clearColor];[shoplabel setTitleColor:[UIColor redColor]forState:UIControlStateNormal];shoplabel.titleLabel.font = [UIFont systemFontOfSize: kTextFontsize1];[shoplabel addTarget:self action:@selector(shopItemClick:) forControlEvents:UIControlEventTouchUpInside];[shoplabel setTag:(indexPath.row*100+goods.ebid)];[cell.goodsAreaView addSubview:shoplabel];if(goods.ebid != 12){ // J1 没有// 显示官网同品类搜索图标/*posx=kScreenWidth - 135;posy=goodsAreaHeight+3;labelwidth=15;labelheight=15;UIButton *websearchlabel = [[UIButton alloc] initWithFrame:CGRectMake(posx,posy,labelwidth,labelheight)];[websearchlabel setImage:[UIImage imageNamed:@"search_k.png"]forState:UIControlStateNormal];[websearchlabel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];websearchlabel.backgroundColor = [UIColor clearColor];[websearchlabel setTitleColor:[UIColor redColor]forState:UIControlStateNormal];websearchlabel.titleLabel.font = [UIFont systemFontOfSize: kTextFontsize1];[websearchlabel addTarget:self action:@selector(shopSearchItemClick:) forControlEvents:UIControlEventTouchUpInside];[websearchlabel setTag:(indexPath.row*100+goods.ebid)];[cell.goodsAreaView addSubview:websearchlabel];*/// 显示官网同品类搜索posx=kScreenWidth - 145;posy=goodsAreaHeight;labelwidth=120;labelheight=kItemcolheight;//NSLog(@"官网同品类搜索 shopAreaHeight:%d,goodsAreaHeight:%d,posy:%d,labelheight:%d",shopAreaHeight,goodsAreaHeight,posy,labelheight);UIButton *shopsearchlabel = [[UIButton alloc] initWithFrame:CGRectMake(posx,posy,labelwidth,labelheight)];[shopsearchlabel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];[shopsearchlabel setTitle:[_appDelegate getShopSearchNameByEBID:goods.ebid] forState: UIControlStateNormal];//NSLog(@"显示官网同品类搜索:%@",[_appDelegate getShopSearchNameByEBID:goods.ebid]);shopsearchlabel.backgroundColor = [UIColor clearColor];[shopsearchlabel setTitleColor:self.view.tintColor forState:UIControlStateNormal];shopsearchlabel.titleLabel.font = [UIFont systemFontOfSize: kTextFontsize1];[shopsearchlabel addTarget:self action:@selector(shopSearchItemClick:) forControlEvents:UIControlEventTouchUpInside];[shopsearchlabel setTag:(indexPath.row*100+goods.ebid)];[cell.goodsAreaView addSubview:shopsearchlabel];}goodsAreaHeight+=labelheight*1.5;}rowindex=floor((double)oneebidgoodsnum / _kShopGoodsCols);if(rowindex > displayRowIndexMax){if(goods.displayMore){if(!moreItemDisplayed){// 显示“收起”moreItemDisplayed=true;int posx=(_kShopGoodsWidth-kScreenShopGoodsPicWidth)/2;int posy=goodsAreaHeight+rowindex*kOneGoodsHight;int labelwidth=200;int labelheight=kItemcolheight;UIButton *morelabel = [[UIButton alloc] initWithFrame:CGRectMake(posx,posy,labelwidth,labelheight)];[morelabel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];[morelabel setTitle:[NSString stringWithFormat:@"收起 < "] forState: UIControlStateNormal];morelabel.backgroundColor = [UIColor clearColor];[morelabel setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];morelabel.titleLabel.font = [UIFont systemFontOfSize: kTextFontsize1];[morelabel addTarget:self action:@selector(goodsMoreItemClick:) forControlEvents:UIControlEventTouchUpInside];[morelabel setTag:(indexPath.row*100+goods.ebid)];[cell.goodsAreaView addSubview:morelabel];}}else{if(!moreItemDisplayed){// 显示“更多”moreItemDisplayed=true;int posx=(_kShopGoodsWidth-kScreenShopGoodsPicWidth)/2;int posy=goodsAreaHeight+rowindex*kOneGoodsHight;int labelwidth=200;int labelheight=kItemcolheight;UIButton *morelabel = [[UIButton alloc] initWithFrame:CGRectMake(posx,posy,labelwidth,labelheight)];[morelabel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];[morelabel setTitle:[NSString stringWithFormat:@"更多 > "] forState: UIControlStateNormal];morelabel.backgroundColor = [UIColor clearColor];[morelabel setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];morelabel.titleLabel.font = [UIFont systemFontOfSize: kTextFontsize1];[morelabel addTarget:self action:@selector(goodsMoreItemClick:) forControlEvents:UIControlEventTouchUpInside];[morelabel setTag:(indexPath.row*100+goods.ebid)];[cell.goodsAreaView addSubview:morelabel];oneebGoodsAreaHeight=moreItemHeight+(rowindex)*kOneGoodsHight;}continue;}}{// 显示一个商家的商品列表rowindex=floor((double)oneebidgoodsnum / _kShopGoodsCols);colindex=oneebidgoodsnum % _kShopGoodsCols;int curgoodstopy=goodsAreaHeight+rowindex*kOneGoodsHight;oneebGoodsAreaHeight=(rowindex+1)*kOneGoodsHight;if(rowindex > displayRowIndexMax){curgoodstopy+=moreItemHeight;oneebGoodsAreaHeight=moreItemHeight+(rowindex+1)*kOneGoodsHight;}//NSLog(@"show goods oneebidgoodsnum=%d,_kShopGoodsCols:%d,rowindex:%d,colindex:%d",oneebidgoodsnum,_kShopGoodsCols,rowindex,colindex);int posx=_kShopGoodsWidth*colindex+(_kShopGoodsWidth-kScreenShopGoodsPicWidth)/2;int posy=curgoodstopy;int labelwidth=kScreenShopGoodsPicWidth;int labelheight=kScreenShopGoodsPicHeight;//NSLog(@"图片:x:%d,y:%d,w:%d,h:%d",posx,posy,labelwidth,labelheight);UIView *picView = [[UIControl alloc] initWithFrame:CGRectMake(posx,posy,labelwidth,labelheight)] ;picView.backgroundColor = [UIColor clearColor];[(UIControl *)picView addTarget:self action:@selector(goodsClick:) forControlEvents:UIControlEventTouchUpInside];UIImageView *goodsPicView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,labelwidth,labelheight)];[goodsPicView sd_setImageWithURL:[NSURL URLWithString:goods.pic] placeholderImage:[UIImage imageNamed:@"yxshpic_default.png"]];//yxshpic_default[(UIControl *)picView addSubview:goodsPicView];[(UIControl *)picView setTag:(indexPath.row*1000+i)];[cell.goodsAreaView addSubview:picView];posy=curgoodstopy+kScreenShopGoodsPicHeight;labelwidth=kScreenShopGoodsTextWidth;labelheight=kScreenShopGoodsTextHeight;//NSLog(@"文字:x:%d,y:%d,w:%d,h:%d",posx,posy,labelwidth,labelheight);UIButton *goodstextlabel = [[UIButton alloc] initWithFrame:CGRectMake(posx,posy,labelwidth,labelheight)];[goodstextlabel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];[goodstextlabel setTitle:[NSString stringWithFormat:@"%d.%@",(oneebidgoodsnum+1),goods.name] forState: UIControlStateNormal];goodstextlabel.backgroundColor = [UIColor clearColor];goodstextlabel.titleLabel.lineBreakMode=0;[goodstextlabel setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];goodstextlabel.titleLabel.font = [UIFont systemFontOfSize: kTextFontsize1];[goodstextlabel addTarget:self action:@selector(goodsClick:) forControlEvents:UIControlEventTouchUpInside];[goodstextlabel setTag:(indexPath.row*1000+i)];[cell.goodsAreaView addSubview:goodstextlabel];posy=curgoodstopy+kScreenShopGoodsPicHeight+kScreenShopGoodsTextHeight;labelwidth=kScreenShopGoodsPriceWidth;labelheight=kScreenShopGoodsPriceHeight;//NSLog(@"价格:x:%d,y:%d,w:%d,h:%d",posx,posy,labelwidth,labelheight);UIButton *goodspricelabel = [[UIButton alloc] initWithFrame:CGRectMake(posx,posy,labelwidth,labelheight)];[goodspricelabel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];if(price > 0){[goodspricelabel setTitle:[NSString stringWithFormat:@"%@:¥%.0f",shopname,price] forState: UIControlStateNormal];}else{[goodspricelabel setTitle:[NSString stringWithFormat:@"%@:查看",shopname] forState: UIControlStateNormal];}goodspricelabel.backgroundColor = [UIColor clearColor];[goodspricelabel setTitleColor:[UIColor redColor]forState:UIControlStateNormal];goodspricelabel.titleLabel.font = [UIFont systemFontOfSize: kTextFontsize1];[goodspricelabel addTarget:self action:@selector(goodsClick:) forControlEvents:UIControlEventTouchUpInside];[goodspricelabel setTag:(indexPath.row*1000+i)];[cell.goodsAreaView addSubview:goodspricelabel];posx=_kShopGoodsWidth*colindex+(_kShopGoodsWidth-kScreenShopGoodsPicWidth)/2+80;posy=curgoodstopy+kScreenShopGoodsPicHeight+kScreenShopGoodsTextHeight-12;labelwidth=kScreenShopGoodsPriceWidth;labelheight=kScreenShopGoodsPriceHeight;if(price > 0){UIButton *pricenotifylabel = [[UIButton alloc] initWithFrame:CGRectMake(posx,posy,labelwidth,labelheight)];if([_appDelegate isFocused:goods.goodsid]){//NSLog(@"view guanbi");[pricenotifylabel setImage:[UIImage imageNamed:@"jjtzs_h.png"]forState:UIControlStateNormal];goods.focused=true;}else{//NSLog(@"view kaiqi");[pricenotifylabel setImage:[UIImage imageNamed:@"jjtzs_n.png"]forState:UIControlStateNormal];goods.focused=false;}[pricenotifylabel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];pricenotifylabel.backgroundColor = [UIColor clearColor];[pricenotifylabel setTitleColor:[UIColor redColor]forState:UIControlStateNormal];pricenotifylabel.titleLabel.font = [UIFont systemFontOfSize: kTextFontsize1];[pricenotifylabel addTarget:self action:@selector(onClicksUnOrFocus:) forControlEvents:UIControlEventTouchUpInside];[pricenotifylabel setTag:(indexPath.row*1000+i)];[cell.goodsAreaView addSubview:pricenotifylabel];}if(colindex==0 && (i+_kShopGoodsCols)<newArr.count && rowindex!=displayRowIndexMax){posy=curgoodstopy+kScreenShopGoodsPicHeight+kScreenShopGoodsTextHeight+kScreenShopGoodsPriceHeight+5;UIView *tmplineView = [[UIView alloc] initWithFrame:CGRectMake(0, posy, kScreenWidth - kGoodsAreaViewGap*2, 0.7)];tmplineView.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0.85 alpha:0.85];[cell.goodsAreaView addSubview:tmplineView];}oneebidgoodsnum++;}}else{//                NSLog(@"name:%@,price=%d",model.name,goods.price);}}goodsAreaHeight+=oneebGoodsAreaHeight;cell.itemshop.text = [NSString stringWithFormat:@"%@:",itemshopname];if(itemshopprice > 0){cell.itemshopprice.text = [NSString stringWithFormat:@"¥%.0f",itemshopprice];}else{cell.itemshopprice.text = [NSString stringWithFormat:@"查看"];}if(model.ebid != 12){ // J1 没有// 显示官网同品类搜索图标int posx=0;int posy=3;int labelwidth=15;int labelheight=15;/*UIButton *websearchlabel = [[UIButton alloc] initWithFrame:CGRectMake(posx,posy,labelwidth,labelheight)];[websearchlabel setImage:[UIImage imageNamed:@"search_k.png"]forState:UIControlStateNormal];[websearchlabel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];websearchlabel.backgroundColor = [UIColor clearColor];[websearchlabel setTitleColor:[UIColor redColor]forState:UIControlStateNormal];websearchlabel.titleLabel.font = [UIFont systemFontOfSize: 11.0];[websearchlabel addTarget:self action:@selector(shopSearchItemClick:) forControlEvents:UIControlEventTouchUpInside];[websearchlabel setTag:(indexPath.row*100+model.ebid)];[cell.searchAreaView addSubview:websearchlabel];*/// 显示官网同品类搜索posx=0;posy=0;labelwidth=120;labelheight=20;//NSLog(@"官网同品类搜索 shopAreaHeight:%d,goodsAreaHeight:%d,posy:%d,labelheight:%d",shopAreaHeight,goodsAreaHeight,posy,labelheight);UIButton *shopsearchlabel = [[UIButton alloc] initWithFrame:CGRectMake(posx,posy,labelwidth,labelheight)];[shopsearchlabel setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];[shopsearchlabel setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];[shopsearchlabel setTitle:[_appDelegate getShopSearchNameByEBID:model.ebid] forState: UIControlStateNormal];// NSLog(@"item显示官网同品类搜索:%@",[_appDelegate getShopSearchNameByEBID:model.ebid]);shopsearchlabel.backgroundColor = [UIColor clearColor];[shopsearchlabel setTitleColor:self.view.tintColor forState:UIControlStateNormal];shopsearchlabel.titleLabel.font = [UIFont systemFontOfSize: kTextFontsize1];[shopsearchlabel addTarget:self action:@selector(shopSearchItemClick:) forControlEvents:UIControlEventTouchUpInside];[shopsearchlabel setTag:(indexPath.row*100+model.ebid)];[cell.searchAreaView addSubview:shopsearchlabel];}// 降价通知int goodsid=0;goodsModel *goods;arr = model.goodsArray;if(arr){for(int i=0;i<arr.count;i++){goods = [arr objectAtIndex:i];if(goods && goods.ebid==model.ebid){goodsid=goods.goodsid;if(goods.price > 0){if([_appDelegate isFocused:goodsid]){//NSLog(@"view guanbi");[cell.focus setImage:[UIImage imageNamed:@"jjtzs_h.png"]forState:UIControlStateNormal];goods.focused=true;}else{//NSLog(@"view kaiqi");[cell.focus setImage:[UIImage imageNamed:@"jjtzs_n.png"]forState:UIControlStateNormal];goods.focused=false;}}break;}}}[cell.focus setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];cell.focus.backgroundColor = [UIColor clearColor];[cell.focus setTitleColor:[UIColor redColor]forState:UIControlStateNormal];cell.focus.titleLabel.font = [UIFont systemFontOfSize: kTextFontsize1];[cell.focus addTarget:self action:@selector(onClicksUnOrFocus:) forControlEvents:UIControlEventTouchUpInside];[cell.focus setTag:(indexPath.row*1000+eb_goods_max)];}else{
//        NSLog(@"name:%@,goodsarr=nil",model.name);}int newHeight=shopAreaHeight+goodsAreaHeight;//NSLog(@"newHeight:%d,shopAreaHeight:%d,goodsAreaHeight:%d",newHeight,shopAreaHeight,goodsAreaHeight);CGRect rect = cell.frame;rect.origin.x = rect.origin.x;rect.origin.y = rect.origin.y;rect.size.height = kItemstartypos+newHeight+10;cell.frame=rect;CGRect lineRect = cell.lineView.frame;lineRect.origin.x = lineRect.origin.x;lineRect.origin.y = kItemstartypos+newHeight+10;lineRect.size.height = lineRect.size.height;cell.lineView.frame=lineRect;CGRect goodsrect = cell.goodsAreaView.frame;goodsrect.origin.x = goodsrect.origin.x;goodsrect.origin.y = kItemstartypos+shopAreaHeight+1;goodsrect.size.height = goodsAreaHeight+1;cell.goodsAreaView.frame=goodsrect;CGRect shoprect = cell.shopAreaView.frame;shoprect.origin.x = shoprect.origin.x;shoprect.origin.y = shoprect.origin.y;shoprect.size.height = shopAreaHeight+1;cell.shopAreaView.frame=shoprect;return cell;
}工程完整代码加扣群:385378208 即可下载

转载于:https://www.cnblogs.com/srf123/p/6133201.html

iOS开发总结-UITableView 自定义cell和动态计算cell的高度相关推荐

  1. iOS开发系列--UITableView全面解析

    iOS开发系列--UITableView全面解析 2014-08-23 23:20 by KenshinCui, 2202 阅读, 18 评论, 收藏,  编辑 --UIKit之UITableView ...

  2. iOS开发多线程篇—自定义NSOperation

    iOS开发多线程篇-自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UI ...

  3. Android 动态计算ListView的高度

    目录 一.简介 二.效果图 三.代码实现 一.简介 在Android开发的过程中有的时候我们需要手动计算ListView的高度,比如说,ScrollView中嵌套ListView的时候,我们就需要手动 ...

  4. iOS开发那些事--自定义单元格实现

    自定义单元格 当苹果公司提供给的单元格样式不能我们的业务需求的时候,我们需要自定义单元格.在iOS 5之前,自定义单元格可以有两种实现方式:代码实现和用xib技术实现.用xib技术实现相对比较简单,创 ...

  5. iOS开发学无止境 - UICollectionView自定义布局之风火轮[译]

    现在有许多极具创造力的网站,几周前我碰巧浏览到一个名为Form Follows Function的网站,上面有各种交互动画.其中最吸引我的是网站上的导航转轮,转轮由各种交互体验海报组成. 原文:UIC ...

  6. iOS开发使用UIKeyInput自定义密码输入框

    前言 开发中很多地方都会遇到密码输入,这时候往往需要根据UI设计自定义.这里遵守UIKeyInput,实现协议中的方法,让自定义View可以进行文字输入:再通过func draw(_ rect: CG ...

  7. ios开发之--UITableView中的visibleCells的用法

    先上图: 具体代码如下: #import "ViewController.h"@interface ViewController ()<UITableViewDelegate ...

  8. iOS开发小结 - 使用自定义字体

    APP开发过程中,根据美工的需求可能会用到一些特殊的字体,然而在苹果自带字体中并没有,那就必须我们开发来来实现这些功能呢,下面以冬青黑体简体中文(Hiragino Sans GB)为例子给大家演示一下 ...

  9. iOS开发有关app启动时的动态页面展示(非启动页面)

    很多APP启动的时候,需要动态的更换启动页面,可以在这个地方增添广告或者是将APP中新加入的一些活动信息等展示给用户.废话不多说,上代码: 一般都是在AppDelegate中的这个方法进行实现,当然这 ...

最新文章

  1. asp.net简单的注册或者登陆三层架构的例子
  2. 寻根求源 U盘的9个典型故障
  3. Web项目中引进EasyUI的路径问题
  4. C++如何使用MySQL数据库
  5. glibc free 死锁
  6. 如何在XSLT中将字符串转换为大写或小写形式
  7. 李想发布了一款怎样的智能电动车?
  8. 瑞波基因币靠谱吗_Fil币小矿工: IPFS国家认可吗?IPFS是靠谱项目吗?
  9. [NOI2017]整数
  10. httpclient4.x 中文版帮助文档,最新官方版翻译版(第一章 下)
  11. 【C代码】结构体数组初始化的相关总结
  12. 从Soul APP 看社交升级新玩法
  13. Android dp2px
  14. oppo手机禁止自动打开应用商店
  15. gtk下的messagebox
  16. [frida] 01_食用指南(持续更新)
  17. 斗鱼实名认证 mysql_斗鱼怎么进行实名认证_斗鱼实名认证方法教程-游戏窝
  18. 【Python学习记录】Numpy广播机制(broadcast)
  19. Java为什么需要数据类型
  20. 图形界面 I: 图形界面的动画 (第三章)

热门文章

  1. 超星集团武汉研发中心面试题
  2. 学计算机还要写作文吗,学电脑写作文
  3. 开发AndroidStudio图片压缩插件TinyPngPlus
  4. java 生成word文件怎么打开乱码_打开word文档 乱码弹出文件转换对话框
  5. GEE——土地利用分类之监督分类
  6. 高分辨率遥感影像融合技巧
  7. 用SEO五招轻松搞定竞争对手
  8. Python绘制各大城市职工工资分布地图
  9. MFC基于对话框添加图片控件
  10. python中用*编写直角三角形