创建单元格

1.

1)新建.xib文件,且设置好想要的效果

2)在属性栏中设置Identifier属性,属性值将在代码中用到。

 

2.新建SimpleTabelCell.m和SimpleTabelCell.h文件,且继承UITableViewCell类。

3.定义三个变量

.h中:

 

@interfaceSimpleTabelCell : UITableViewCell

//定义三个实例变量

@property(nonatomic,weak)IBOutlet UILabel*nameLabel;

@property(nonatomic,weak)IBOutlet UILabel*prepTimeLabel;

@property(nonatomic,weak)IBOutlet UIImageView*thumbnailImageView;

@end

 

.m中:

 

#import "SimpleTabelCell.h"

 

@implementationSimpleTabelCell

//@synthesize关键字告诉编译器自动生成代码,用来访问前面定义的属性。

@synthesizenameLabel = _nameLabel;

@synthesizeprepTimeLabel = _prepTimeLabel;

@synthesizethumbnailImageView = _thumbnailImageView;

 

- (id)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithStyle:stylereuseIdentifier:reuseIdentifier];

    if (self) {

        //Initialization code

    }

    return self;

}

 

- (void)setSelected:(BOOL)selectedanimated:(BOOL)animated

{

    [super setSelected:selected animated:animated];

 

    // Configurethe view for the selected state

}

 

@end

 

4.ViewController中修改代码:

如下:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{//提供表视图单元格所需要的数据。   

    static NSString* simpleTableIdentifier = @"SimpleTableItem";

    //该语句为表视图单元格提供了一个标识,当上面的单元格滚出屏幕,下面的单元格滚入屏幕时候,可以通过判断这个标识是否有可以重用的单元格,如果有则重用,如果没有则创建一个新的。

   

    SimpleTabelCell* cell = (SimpleTabelCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell== nil) {

        NSArray*nib = [[NSBundle mainBundle]loadNibNamed:@"SimpleTabelCell"owner:selfoptions:nil];

        cell = [nib objectAtIndex:0];

    }

 

    cell.nameLabel.text =  [tabelData objectAtIndex:indexPath.row];

    cell.thumbnailImageView.image = [UIImageimageNamed:@"MS10020200_p02_btn_02_all_4"];

    returncell;

}

 

.xib与.storyboard结合使用时:要加Identifier值

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

{

    NSString *CellIdentifier ;

    //自己子类化的UITableViewCell

    PersonTableCell * lableSwitchCell;

    UINib *n;

    if([indexPath section ] == 0 ) {

        CellIdentifier = @"PersonTableXibCell";

        lableSwitchCell  = (PersonTableCell*)[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

        if(lableSwitchCell == nil) {

            NSArray*_nib=[[NSBundle mainBundle]loadNibNamed:@"PersonTableCell"

                                                       owner:self  options:nil];

            lableSwitchCell  = [_nib objectAtIndex:0];

            //通过这段代码,来完成LableSwitchXibCellReuseIdentifier的设置

            //这里是比较容易忽视的,若没有此段,再次载入LableSwitchXibCell时,dequeueReusableCellWithIdentifier:的值依然为nil

            n= [UINibnibWithNibName:@"PersonTableCell"bundle:[NSBundlemainBundle]];

            [self.tableView registerNib:nforCellReuseIdentifier:@"PersonTableXibCell"];

        }

    }

    returnlableSwitchCell;

   

}

 

 

 

并添加函数:《因为自己设置的单元格高度改变了。所以要此方法》

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{

    return 78;

}

 

5。点击某个单元格时,添加事件。(重写方法)

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

 

}

 

 

6.选中状态:《支持单行选择

1)在controller.m中添加

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{//提供表视图单元格所需要的数据。   

……

 //支持单行选择

    NSIndexPath*selection = [tableView indexPathForSelectedRow];

    if(selection && selection.row ==indexPath.row) {

        cell.accessoryType= UITableViewCellAccessoryCheckmark;

    }else{

        cell.accessoryType= UITableViewCellAccessoryNone;

    }

……

2)在- (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{

……

SimpleTabelCell *cell= (SimpleTabelCell*)[tableView cellForRowAtIndexPath:indexPath];cell.accessoryType = UITableViewCellAccessoryCheckmark;

……

3)在controller.m中添加方法

-(void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath{

 

    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;

}

7.选中状态:《支持多行选择

 

 

 

 

调用 tableView 对象的 indexPathForSelectedRow 方法, 迒回表规图中所有选择的行。

 

任何时候在一个对象需要完成特定任务时,它依赖于另外一个对象去负责处理。这个在系统设计领域通常称为关注点分离(Separation of Concern)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值