UITable View的常用代理方法

//设置行高

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

    return 80;
    }
    //分区

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    // Return the number of sections.
    return 3;
    }
    //设置每个区有多少行共有多少行

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return 2;
    }

//设置区域的名称

  • (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
    {
    return @"123";
    }

//是否允许行移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{

return YES;

}

//响应点击事件

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSLog(@"响应单击事件");
    }

//小按钮的响应事件

  • (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
    {
    NSLog(@"accessoryButton的响应事件");

}

//删除按钮的名字
-(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"删除";
}
//设置滑动,
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
//ruturn NO不实现滑动
return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"手指撮动了");
return UITableViewCellEditingStyleDelete;
// //插入
// return UITableViewCellEditingStyleInsert;
}
设置CELL的样式

// cell.selectionStyle = UITableViewCellSelectionStyleBlue;
//灰色
// cell.selectionStyle = UITableViewCellSelectionStyleGray;
//无颜色
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
//向右箭头样式
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//向右箭头button
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

UITableViewCellStyleDefault,// Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
UITableViewCellStyleValue1,// Left aligned label on left and right aligned label on right with blue text (Used in Settings)
UITableViewCellStyleValue2,// Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
UITableViewCellStyleSubtitle

UITableView 总结

UITableView是UIScrollView的子类,因此它可以自动响应滚动事件(一般为上下滚动)。
它内部包含0到多个UITableViewCell对象,每个table cell展示各自的内容。
当新cell需要被显示时,就会调用tableView:cellForRowAtIndexPath:方法来获取或创建一个cell;
而不可视时,它又会被释放。
由此可见,同一时间其实只需要存在一屏幕的cell对象即可,不需要为每一行创建一个cell。

两种样式:group 和 plain

1.协议介绍

UITableViewDataSource(11)

//每个section下cell的个数(必须实现)

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
    //通过indexpath返回具体的cell(必须实现)
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

//返回有多少个section(默认是1)

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
    //每个section上面的标语内容
  • (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
    //每个section下面的标语内容
  • (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

// Editing
//是否可编辑

  • (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

// Moving/reordering
// 是否可拖拽
-tableView:moveRowAtIndexPath:toIndexPath:

  • (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

// Index
//右侧索引条需要的数组内容

  • (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
    // return list of section titles to display in section index view (e.g. "ABCD...Z#")

//索引值对应的section-index

  • (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index; // tell table which section corresponds to section title/index (e.g. "B",1))

// Data manipulation - insert and delete support
// 对Cell编辑后的回调

  • (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

// 对Cell拖拽后的回调

  • (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
    UITableViewDelegate(常用)

//将要展示Cell/header/Footer视图回调

  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
  • (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
  • (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
    //完成展示Cell/header/Footer视图回调
  • (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell )cell forRowAtIndexPath:(NSIndexPath)indexPath NS_AVAILABLE_IOS(6_0);
  • (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
  • (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);

// Variable height support
// 每个cell高度的返回(这里高度通过协议返回,是为了table能准确的定位出要显示的Cell-index,从而满足UITableView的重用机制)

  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
    // 每个section-header高度的返回
  • (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
    // 每个section-footer高度的返回
  • (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

// Section header & footer information. Views are preferred over title should you decide to provide both
//可返回每个section-header的自定义视图

  • (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; // custom view for header. will be adjusted to default or specified header height
    //可返回每个section-footer的自定义视图
  • (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; // custom view for footer. will be adjusted to default or specified footer height

// Selection

// Cell高亮的回调,一般式在选择的时候才高亮

  • (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
  • (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
  • (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);

// Cell选中和取消选择的回调

  • (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
  • (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
    // Called after the user changes the selection.
  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
  • (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
    UITableViewDelegate中的协议还有很多,我只列出了比较常用的,想知道更多的可以查看官方头文件或官方文档

2.下拉刷新 上拉加载
MJRefresh

3.重用
UItableView对Cell有一套重用机制,他会将滚出屏幕外的cell放到一个队列中,滚入屏幕的会从这个队列中获取cell,如果没有再去创建。

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

{

static NSString *cellIdentifier = @"cellIdentifier ";



UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:windowReuseIdentifier];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:windowReuseIdentifier];

    }

return cell;

}

自定义cell

新建cell文件,继承UITableViewCell

static NSString *cellid = @"cellIdentifier";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];

if (!cell) {
    cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
}
  1. 如果有xib

static NSString *CellIdentifier = @"FriendCell";
FriendCell *cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
[tableView registerNib:[UINib nibWithNibName:@"FriendCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}

不使用重用方法

方法1 将获得cell的方法从- (UITableViewCell)dequeueReusableCellWithIdentifier:(NSString)identifier 换为-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

重用机制调用的就是dequeueReusableCellWithIdentifier这个方法,方法的意思就是“出列可重用的cell”,因而只要将它换为cellForRowAtIndexPath(只从要更新的cell的那一行取出cell),就可以不使用重用机制,因而问题就可以得到解决,虽然可能会浪费一些空间。

第一个方法如果使用下面插入多次可能会有问题:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
方法2 通过为每个cell指定不同的重用标识符(reuseIdentifier)来解决。

重用机制是根据相同的标识符来重用cell的,标识符不同的cell不能彼此重用。于是我们将每个cell的标识符都设置为不同(@"CMainCell%d", indexPath.row)

5.编辑

//是否可编辑

  • (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

//返回编辑的类型1.没有2.删除3.插入

  • (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

//删除提示文本

  • (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);

//编辑完成

  • (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
    滑动更多

.尽量不要老调用reloaddata,可能的情况下可以考虑使用

  • (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

  • (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

  • (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);

  • (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection NS_AVAILABLE_IOS(5_0);

  • (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

  • (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

  • (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
    -(void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);

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

推荐阅读更多精彩内容