* tableView继承自scrollView
我们需要设置数据源才能显示数据,设置数据源和代理很类似,三步(
1.遵循数据源协议
2.设置数据源
3.实现数据源方法
头部和尾部标题 (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
* cel的accessoryView属性必须要设置大小
cell的的backgroundView以及selectBackgroundView
设置cell的分割线距离上左下右的距离,一般上和下边没有效果
cell.separatorInset = UIEdgeInsetsMake(20, 0, 30, 40);
*tableView的属性:分割线的样式 (seperatestyle)
1.带着一个重用的标示符去缓存池中取重用cell
static NSString *ID = @"hero"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; 2.如果没有重用的cell,我们创建一个 cell if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; }
3.给cell设置数据覆盖原来的数据
HeroModel *hero = self.heros[indexPath.row];
// 3.1设置名称
cell.textLabel.text = hero.name;
//// 3.2设置图片
cell.imageView.image = [UIImage imageNamed: hero.icon];
//// 3.3设置子标题
cell.detailTextLabel.text = hero.intro;
//// 3.4设置指示器
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
刷新数据(两个步骤) 1>修改模型 2>刷新数据(两种方式:一种全局刷新,一种局部刷新,局部刷新性能较高) [self.tableView reloadData];//全局刷新 2.1局部刷新 [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]
7.删除插入cell(总体原则,修改模型,刷新数据)(实现三个方法)
/**
* 表示tableView能够进行编辑 * */ - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } /** * 表示编辑模式,当我们想要进行cell的插入和删除操作的时候就会调用这个方法 * */ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleDelete ) { // 1.修改模型(删除模型) HeroModel *hero = self.heros[indexPath.row]; [self.heros removeObject:hero]; // 2.刷新数据 [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; }else if (editingStyle == UITableViewCellEditingStyleInsert){ // 1.修改模型(插入模型) HeroModel *hero = [[HeroModel alloc]init]; hero.name = @"大家都很精神"; [self.heros insertObject:hero atIndex:indexPath.row]; // 2.刷新 [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; } } /** * 表示修改编辑模式 * */ - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleInsert; }
copy, paste cell中的数据(三个方法必须实现) /** * 表示能够让弹出粘贴面板 * */ - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } //表示能够执行复制粘贴剪切的方法 - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{ return YES; } /** * / * 表示执行action(复制,粘贴,拷贝)方法 */ - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{ // 1.去得模型 HeroModel *hero = self.heros[indexPath.row]; // 1.1给粘贴面板赋值 if (action == @selector(copy:)) { [UIPasteboard generalPasteboard].string = hero.icon; } if (action == @selector(paste:)) { hero.icon = [UIPasteboard generalPasteboard].string; [self.tableView reloadData]; } }
原文:http://www.cnblogs.com/gp886/p/4941012.html