一些概念:
1.//滑动删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[dataArray[indexPath.section] removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
2. //去掉尾部多余的cell线条 *****
table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
3.//开启多选编辑 并且不会和侧滑删除冲突
table.allowsMultipleSelectionDuringEditing = YES;
4.#pragma mark ---设置索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return titleArray;
}
5.#pragma mark ---删除
- (void)pressDeleteBtn
{
//获得删除数组
deleteArray = [NSMutableArray arrayWithArray:[table indexPathsForSelectedRows]];
NSInteger count = deleteArray.count;
//冒泡排序遍历 删除数组
//注:如果先删小下标再删大下标的话 数组会越界 所以我们在删除的时候需要从大下标开始删除
for (NSInteger i = 0; i < count; ++i)
{
for (NSInteger j = i+1; j < count; ++j)
{
NSIndexPath *indexP1 = deleteArray[i];
NSIndexPath *indexP2 = deleteArray[j];
if (indexP1.row < indexP2.row)
{
[deleteArray exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
}
for (NSIndexPath *indexPath in deleteArray)
{
[dataArray[indexPath.section] removeObjectAtIndex:indexPath.row];
}
for (NSInteger i = dataArray.count - 1 ; i >=0 ; --i)
{
NSArray *tempArray = dataArray[i];
if (!tempArray.count)
{
[dataArray removeObjectAtIndex:i];
[titleArray removeObjectAtIndex:i];
}
}
[table reloadData];
//每次删完后清空数组
[deleteArray removeAllObjects];
}
6.#pragma mark ---插入
- (void)pressInsertBtn
{
[dataArray[1] insertObject:@"提莫" atIndex:2];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:1];
//刷新单个section
[table insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
//刷新整个表格
// [table reloadData];
}
7#pragma mark ---编辑
- (void)pressEditBtn
{
//检测删除数组中是否有元素
if (deleteArray.count)
{
[deleteArray removeAllObjects];
}
if (table.editing)
{
[table setEditing:NO animated:YES];
}
else
{
[table setEditing:YES animated:YES];
}
}
8. //设置cell的分离样式
/* UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine,
UITableViewCellSeparatorStyleSingleLineEtched
*/
tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
9.- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//一.复用并创建cell
//1.创建cell的标示符
static NSString *cellID = @"cellID";
//2.去复用池中取cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
//3.如果取出的cell为nil,那么我们就需要创建cell
if (cell == nil)
{
//如果需要使用系统自带的子标题控件,那么cell的样式必须设置为 UITableViewCellStyleSubtitle
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
/*
UITableViewCellSelectionStyleNone,无选中效果
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray,
UITableViewCellSelectionStyleDefault
*/
//设置cell的选中样式
cell.selectionStyle = UITableViewCellSelectionStyleGray;
/*
UITableViewCellAccessoryNone, 无 默认
UITableViewCellAccessoryDisclosureIndicator, 箭头
UITableViewCellAccessoryDetailDisclosureButton,
UITableViewCellAccessoryCheckmark,
UITableViewCellAccessoryDetailButton
*/
//设置系统的右侧小图标
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
//设置自定义的右侧图标(文字)
UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
imageV.image = [UIImage imageNamed:@"1.jpg"];
cell.accessoryView = imageV;
//设置编辑状态下的右侧小图标
cell.editingAccessoryType = UITableViewCellAccessoryCheckmark;
// editingAccessoryView 设置编辑状态下自定义的右侧小图标
//设置自定义的选中的背景图片
UIImageView *backView = [[UIImageView alloc]init];
backView.image = [UIImage imageNamed:@"map"];
cell.selectedBackgroundView = backView;
}
//二.取数据
NSDictionary *dic = self.dataArray[indexPath.section][indexPath.row];
//三.给cell赋值 有if 加else
//textLabel
cell.textLabel.text = dic[@"userName"];
if ([dic[@"vip"] isEqualToString:@"1"])
{
cell.textLabel.textColor = [UIColor redColor];
}
else
{
cell.textLabel.textColor = [UIColor blackColor];
}
cell.imageView.image = [UIImage imageNamed:dic[@"userImage"]];
cell.imageView.clipsToBounds = YES;
cell.imageView.layer.cornerRadius = 10;
//detailTextLabel是小的label
cell.detailTextLabel.text = dic[@"vip"];
return cell;
}
一些实用技巧:
1.点击section头部,缩放cell
(1).全局 NSMutableArray *indexArray;
(2). indexArray = [NSMutableArray arrayWithObjects:@"1",@"1",@"1",@"1",@"1" ,nil];
(3).- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *isOpen = indexArray[section];
if ([isOpen isEqualToString:@"1"])
{
return [self.dataArray[section] count];
}
else
{
return 0;
}
}
(4).- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSArray *array = @[@"1601班",@"撸友",@"前女友",@"基友",@"好丽友"];
UILabel *label = [[UILabel alloc] init];
label.text = array[section];
label.backgroundColor = [UIColor redColor];
label.userInteractionEnabled = YES;
label.tag = 100 + section;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickHeader:)];
[label addGestureRecognizer:tap];
return label;
}
#pragma mark ---头部点击事件
- (void)clickHeader:(UITapGestureRecognizer *)tap
{
UILabel *label = (id)tap.view;
NSString *stateStr = indexArray[label.tag - 100];
if ([stateStr integerValue] == 1)
{
[indexArray replaceObjectAtIndex:label.tag - 100 withObject:@"0"];
}
else
{
[indexArray replaceObjectAtIndex:label.tag - 100 withObject:@"1"];
}
//刷新数据表格
[tableV reloadData];
}