热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

UITableViewcell自定义头文件:shopCell.h#import@interfaceshopCell:UITableViewCell@pr

技术分享


UITableView cell自定义头文件:

shopCell.h

#import

@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 

@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);
    
    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;m0){
                // 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;m0){
                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;m0){
                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) 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 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的高度


推荐阅读
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • OpenMap教程4 – 图层概述
    本文介绍了OpenMap教程4中关于地图图层的内容,包括将ShapeLayer添加到MapBean中的方法,OpenMap支持的图层类型以及使用BufferedLayer创建图像的MapBean。此外,还介绍了Layer背景标志的作用和OMGraphicHandlerLayer的基础层类。 ... [详细]
  • pythonMatplotlib(二)
    Matplotlib+pandas作图一、对csv文件进行提取ruixi.csv对上述表格进行提取并做图画出图像二、对.xlsx进行提取:rui ... [详细]
  • MVC中的自定义控件
    怎么样创建自定义控 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • CentOS 6.5安装VMware Tools及共享文件夹显示问题解决方法
    本文介绍了在CentOS 6.5上安装VMware Tools及解决共享文件夹显示问题的方法。包括清空CD/DVD使用的ISO镜像文件、创建挂载目录、改变光驱设备的读写权限等步骤。最后给出了拷贝解压VMware Tools的操作。 ... [详细]
  • 我用Tkinter制作了一个图形用户界面,有两个主按钮:“开始”和“停止”。请您就如何使用“停止”按钮终止“开始”按钮为以下代码调用的已运行功能提供建议 ... [详细]
  • 引号快捷键_首选项和设置——自定义快捷键
    3.3自定义快捷键(CustomizingHotkeys)ChemDraw快捷键由一个XML文件定义,我们可以根据自己的需要, ... [详细]
  • 俗话说,好记性不如烂笔头,这些东西也都是我Google来的,做个笔记以后自己安装也方便些。因为官方wiki的BeginnersGuide讲的非常好,大部分步骤按照wiki一步一步来就 ... [详细]
  • 动量|收益率_基于MT策略的实战分析
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了基于MT策略的实战分析相关的知识,希望对你有一定的参考价值。基于MT策略的实战分析 ... [详细]
  • QuestionThereareatotalofncoursesyouhavetotake,labeledfrom0ton-1.Somecoursesmayhaveprerequi ... [详细]
  • Thisissuewasoriginallyopenedbyashashicorp/terraform#5664.Itwasmigratedhe ... [详细]
author-avatar
mobiledu2502937927
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有