【iOS系列】- UITableView的使用技巧

【iOS系列】- UITableView的使用


UITableView的常用属性

indexpath.row:行
indexpath.section:组

separatorColor:分割线的颜色
separatorStyle:分割线样式

用来自定义头部和尾部自定义view
只需要设置高度,宽度设置无效
tableHeaderView;
tableFooterView;

table中展示的数据是从模型(或者其他来源-数据源)中取得的,因此要更改表格数据,只需要更改数据源中得数据,并刷新表格即可。(不要直接更改tableviewcell中textLabel中得text数据,这样上下拖动tableview还是会显示原来的数据)

1:修改模型数据
2:刷新表格

常用的刷新表格的方法

//删除数据,表格的刷新
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];

//插入新数据,表格的刷新
[self.tableView insertRowsAtIndexPaths:(NSArray *) withRowAnimation:(UITableViewRowAnimation)]

//全局刷新
reloadData;

UITableViewCell的常用属性

accessoryType:cell右边的指示器
accessoryView:可自定义右边的view
backgroundView:自定义背景view
backgroundColor//优先级低于backgroundView
selectedBackgroundView


常用代理代理方法

//一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

//每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

//每一行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;


//选中某一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

//取消选中某一行
(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

//刷新表格
[self.tableView reloadData];
//注意IndexPaths参数是NSIndexPath的数组
[self.tableView reloadRowsAtIndexPaths:(NSArray *) withRowAnimation:(UITableViewRowAnimation)]


// 代理方法,实现了这个方法,就会自动实现了滑动删除功能。
/**
 *  @param editingStyle 编辑样式:添加,删除
 *  @param indexPath    删除的行号
 */
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
	//由于tableview还有插入模式,故这里增加判断
    if (editingStyle == UITableViewCellEditingStyleDelete) {//删除操作
    
    } 
}


注:tableView加载完毕后没有数据,一定要设置相应的代理方法

tableView.delegate = self;

tableView.dataSource = self;


xib封装view

1:新建xib描述view的内部结构
2:新建类,继承自view(也取决于xib的跟对象,比如UITableViewCell),
3:把xib的类型修改为新建类的类型,并把xib内部的空间和类进行连线
4:提供类方法,可以返回自定义的view


/**
 *  构造方法,初始化对象时候调用
 *   自定义cell的时候,在这个方法中添加需要显示的子控件
 */
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

//延时操作
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<#delayInSeconds#> * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
code to be executed after a specified delay
});


作者:Darren

微博:@IT_攻城师

github:@Darren90

博客:http://www.cnblogs.com/fengtengfei/

欢迎您的访问...


posted @ 2015-04-13 00:51  Darren.Von  阅读(633)  评论(0编辑  收藏  举报
新浪微博:IT_攻城师,github:darren90(欢迎★star点赞)