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

时间:2022-11-20 23:52:18

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

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
#endif HomeViewController.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 即可下载