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 即可下载
iOS开发总结-UITableView 自定义cell和动态计算cell的高度的更多相关文章
- IOS开发中UITableView(表视图)的滚动优化及自定义Cell
IOS开发中UITableView(表视图)的滚动优化及自定义Cell IOS 开发中UITableView是非常常用的一个控件,我们平时在手机上看到的联系人列表,微信好友列表等都是通过UITable ...
- iOS开发系列--UITableView全面解析
--UIKit之UITableView 概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是U ...
- iOS开发多线程篇—自定义NSOperation
iOS开发多线程篇—自定义NSOperation 一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UI ...
- iOS开发:UITableView的优化技巧-异步绘制Cell
最近在微博上看到一个很好的开源项目VVeboTableViewDemo,是关于如何优化UITableView的.加上正好最近也在优化项目中的类似朋友圈功能这块,思考了很多关于UITableView的优 ...
- iOS开发基础-UITableView控件简单介绍
UITableView 继承自 UIScrollView ,用于实现表格数据展示,支持垂直滚动. UITableView 需要一个数据源来显示数据,并向数据源查询一共有多少行数据以及每一行显示什么 ...
- iOS开发基础-UITableView基本属性
设置 UITableView 中 cell 的背景颜色. 示例1:通过 backgroundView 设置. UIView *view1 = [[UIView alloc] init]; view1. ...
- iOS学习之根据文本内容动态计算文本框高度的步骤
在视图加载的过程中,是先计算出frame,再根据frame加载视图的,所以在设计计算高度的方法的时候,设计成加号方法; //首先给外界提供计算cell高度的方法 + (CGFloat)heightFo ...
- 【ios开发】使用自定义的TableViewCell
当系统自带的cell无法满足我们的要求的时候,我们就可以自定义自己的cell. 先看看效果,这个效果有点重复造轮子的感觉,因为UITableView已经实现了这种布局. 打造自己的cell只需简单的3 ...
- 【转】 iOS 开发之静态库.a和动态库详解 -- 不错
原文网址:http://blog.csdn.net/lxl_815520/article/details/52154331 一, 简单介绍 1.什么是库 库是程序代码的集合,是共享程序代码的一种方式 ...
随机推荐
- Javascript 备忘
1遍历所有属性 var person={fname:"John",lname:"Doe",age:25}; for (x in person) { txt=tx ...
- 7 -- Spring的基本用法 -- 8...
7.8 深入理解容器中的Bean 7.8.1 抽象Bean与子Bean 把多个<bean.../>配置中相同的信息提取出来,集中成配置模版------这个配置模版并不是真正的Bean,因此 ...
- 修改tabbar 字体颜色
NSDictionary *seletedTextAttrs = @{NSForegroundColorAttributeName:[UIColor orangeColor]}; 修改tabbar 字 ...
- APP 如何适应 iPhone 5s/6/6Plus 三种屏幕的尺寸
初代iPhone 2007年,初代iPhone发布,屏幕的宽高是 320 x 480 像素.下文也是按照宽度,高度的顺序排列.这个分辨率一直到iPhone 3GS也保持不变. 那时编写iOS的App( ...
- 第 1 章 策略模式【Strategy Pattern】
第 1 章 策略模式[Strategy Pattern] 以下内容出自: 24种设计模式介绍与6大设计原则.pdf 刘备要到江东娶老婆了,走之前诸葛亮给赵云(伴郎)三个锦囊妙计,说是按天机拆开解决棘手 ...
- 我是如何实用:before :after
本文地址http://www.cnblogs.com/Bond/p/3972854.html 最近一直做移动端,没和IE6打交道了,瞬间感觉世界变美好了.移动端虽然还是各种坑,但是比起修复IE6那还是 ...
- String, StringBuffer, StringBuilder比较
1.见API: String是不可变的字符序列: StringBuffer是线程安全的,可变的字符序列: StringBuilder是可变的字符序列: StringBuffer与String的区别是S ...
- 使AspNetPager控件中文显示分页信息
在日常的编程过程中,很多学员对于使AspNetPager控件中文显示分页信息不是很清楚,本文将由达内的老师为各位学员介绍一下使AspNetPager控件中文显示分页信息的内容. AspNetPager ...
- Android listview.item.clear()与listview.clear()的区别
listview.clear()与listview.item.clear()的区别就是使用了listview.item.clear()后,listview控件中仍然保存着listviewitem项的结 ...
- redis key expire
EXPIRE key seconds 为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除. 在 Redis 中,带有生存时间的 key 被称为『易失的』(volati ...