UITableView常用自定义Cell

用XIB 自定Cell


@interfaceGroupnCell()

@property(weak,nonatomic)IBOutletUIImageView*shopImageView;

@property(weak,nonatomic)IBOutletUILabel*titleLabel;

@property(weak,nonatomic)IBOutletUILabel*priceLabel;

@property(weak,nonatomic)IBOutletUILabel*buyCountLabel;

@end

@implementationGroupnCell

- (void)awakeFromNib {

// Initialization code

}

- (void)setGroupnModel:(GroupnModel*)groupnModel {

//必须对属性进行赋值

_groupnModel= groupnModel;

//对内部的子控件进行赋值

_shopImageView.image= [UIImageimageNamed:groupnModel.icon];

_titleLabel.text= groupnModel.title;

_priceLabel.text= [NSStringstringWithFormat:@"$%@", groupnModel.price];

_buyCountLabel.text= [NSStringstringWithFormat:@"已购买%@", groupnModel.buyCount];

}

@end

注意:记得从XIB加载的时候 系统是调用  - (void)awakeFromNib 方法


主viewController里面关键代码

//每一行要显示的内容

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {

// 1.先定义重用标识符

staticNSString*identifier =@"groupn";

// 2.根据重用标识符到缓存池中去找

GroupnCell*cell = [tableViewdequeueReusableCellWithIdentifier:identifier];

// 3.判断查找到的cell是否为nil

if(nil== cell) {

//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

#warning使用xib的时候,一定不要忘记在xib中设置重用标识符

cell = [[NSBundlemainBundle]loadNibNamed:@"GroupnCell"owner:niloptions:nil].lastObject;

NSLog(@"----");

}

//取出indexPath.row对应的模型

GroupnModel*model =self.dataArray[indexPath.row];

////赋值

//cell.imageView.image = [UIImage imageNamed:model.icon];

//

//cell.textLabel.text = model.title;

//

//cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@", model.price];

returncell;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 1,453评论 9 5
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,217评论 30 472
  • UITableViewCell 父类是UIView UITableView的每一行都是一个UITableViewC...
    翻这个墙阅读 6,682评论 0 1
  • 生辰,一款就连作者自己都觉得很无聊的产品,它几乎没有使用场景,也没有商业价值可言。 出于好奇心,拿来体验了一下,以...
    Muddly阅读 477评论 0 1
  • 为了方便参加@孙尚香Scent 发起的翻包记, 昨天先把自己的化妆包翻过啦,大家可以去看看~ 这就是我昨天乱糟糟的...
    四个二分之一阅读 261评论 0 1