IOS UITableViewCell的使用

概述

UITableViewCell是UITableView的信息展示单元,在药品指南中大量用到,总结一下用法,以便以后简单的使用。

基本属性

UITableViewCell默认自带一个UIImageView,UILabel。这2个一般不能满足我们的需求,经常需要我们自定义cell,

自定义通过

[cell.contentView addSubview:diseaseNameLabel];

类似方法可以添加到cell中。

UITableViewCell的复用

static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

这样可以复用cell,减少内存开支。

但是如果你对每个cell做了特殊的处理,。

if  (index.row  == 0)
{
      //do something,,,
}

要加上index.row的值不等于0的处理,因为是复用的,如果不做处理,会吧index.row == 0的效果也会出现的。

选中效果

UITableViewCell自带3中选中效果

typedef enum {
    UITableViewCellSelectionStyleNone,
    UITableViewCellSelectionStyleBlue,
    UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle;

但是仍提供了自定义选中效果的方法。

// Default is nil for cells in UITableViewStylePlain, and non-nil for UITableViewStyleGrouped. The 'selectedBackgroundView' will be added as a subview directly above the backgroundView if not nil, or behind all other views. It is added as a subview only when the cell is selected. Calling -setSelected:animated: will cause the 'selectedBackgroundView' to animate in and out with an alpha fade.

@property(nonatomic,retain) UIView                *selectedBackgroundView;

这个是cell在选中是背景的实体,我们可以自定义之。

accessoryType

cell的accessoryType

typedef enum {
    UITableViewCellAccessoryNone,                   // don't show any accessory view
    UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track
    UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks
    UITableViewCellAccessoryCheckmark               // checkmark. doesn't track
} UITableViewCellAccessoryType;

同样也提供了自定义的方法

@property(nonatomic,retain) UIView                 *accessoryView;              // if set, use custom view. ignore accessoryType. tracks if enabled can calls accessory action

在xib中定义cell

在tableview的方法

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

中添加代码

{
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
        [cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; 

    }

loadNibNamed的参数是cell的xib文件名。然后我们可以直接使用我们自定义的cell。

自定义背景

UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.frame];
    [imageView setImage:[UIImage imageNamed:@"test.png"]];
    cell.backgroundView = imageView;

可以改变cell的背景。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值