黑马程序员-IOS学习笔记-UITableView、UITableViewCell、UITableViewController

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------


一、自定义UITableViewCell

一般有两种方式:

1.用一个xib文件来描述UITableViewCell的内容,在设置字符串标示,以便重用。

2.通过代码往UITableVeiwCell的contentView中添加子视图,在初始化方法(比如:init,intiwithstyle:reuseidentifier:)中添加子控件,在layoutStyleviews方法中分配子控件的位置和大小。


二、UITableView的编辑模式

1.UITableView有个editing属性,设置为YES时,可以进入编辑模式。在编辑模式下,可以管理表格中的行,比如改变行的排列顺序、增加行、删除行,但不能修改行的内容。
多种方式开启编辑模式

@property(nonatomic,getter=isEditing) BOOL editing
- (void)setEditing:(BOOL)editing animated:(BOOL)animated

2.删除UITableView的行

 1> 首先要开启编辑模式

 2> 实现UITableViewDataSource的如下方法:


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 如果UITableView提交的是删除指令
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 删除真实数据
         // [self.data removeObjectAtIndex:indexPath.row];
        // 删除UITableView中的某一行(带动画效果)
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        // 如果不考虑动画效果,也可以直接[tableView reload];
    }

3.移动UITableView的行

 1> 首先要开启编辑模式

 2> 实现UITableViewDataSource的如下方法(如果没有实现此方法,将无法换行)


- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    int from = sourceIndexPath.row;
    int to = destinationIndexPath.row;
    if (from == to) return;
    // 交换数据
    // [self.data exchangeObjectAtIndex:from withObjectAtIndex:to];
}


4.选中UITableView的行

1> 当某行被选中时会调用此方法(UITableViewDelegate的方法)


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //取消选中某一行,让被选中行的高亮颜色消失(带动画效果)
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


三、UITableView的常用方法

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style  初始化一个UITableView,并且设置表格样式
- (void)reloadData   重新访问数据源,刷新界面
- (NSInteger)numberOfSections  分区的个数
- (NSInteger)numberOfRowsInSection:(NSInteger)section  第section分区的行数
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath  通过indexPath找到对应的UITableViewCell对象
- (void)setEditing:(BOOL)editing animated:(BOOL)animated  是否要开启编辑模式
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated  取消选中某一行,让被选中行的高亮颜色消失(带动画效果)

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier  通过identifier在(缓存)池中找到对应的UITableViewCell对象
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation  移除indexPaths范围内的所有行


@property(nonatomic,readonly) UITableViewStyle style 表格样式
@property(nonatomic,assign) id <UITableViewDataSource> dataSource  数据源
@property(nonatomic,assign) id <UITableViewDelegate> delegate 代理
@property(nonatomic,getter=isEditing) BOOL editing 是否为编辑模式
@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle  设置分隔线的样式
@property(nonatomic,retain) UIColor *separatorColor  设置分隔线的颜色

@property(nonatomic,retain) UIView *tableHeaderView  表头显示的视图
@property(nonatomic,retain) UIView *tableFooterView  表尾显示的视图
@property(nonatomic) BOOL allowsSelection  是否允许选中行
@property(nonatomic) BOOL allowsSelectionDuringEditing  是否允许在编辑模式下选中行
@property(nonatomic) BOOL allowsMultipleSelection  是否允许选中多行
@property(nonatomic) BOOL allowsMultipleSelectionDuringEditing  是否允许在编辑模式下选中多行


四、UITableViewController

1.是UIViewController的子类,UITableViewController默认扮演了3种角色:视图控制器、UITableView的数据源和代理。


2.UITableViewController的view是个UITablView,由UITableViewController负责设置和显示这个对象。UITableViewController对象被创建后,会将这个UITableView对象的dataSource和delegate指向UITableViewController自己。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值