UI第十一天
来源:互联网 发布:淘宝网返利如何领取 编辑:程序博客网 时间:2024/04/27 20:30
一些概念:
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 = [[UIViewalloc] initWithFrame:CGRectZero];
3.//开启多选编辑 并且不会和侧滑删除冲突
table.allowsMultipleSelectionDuringEditing =YES;
4.#pragma mark ---设置索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
returntitleArray;
}
5.#pragma mark ---删除
- (void)pressDeleteBtn
{
//获得删除数组
deleteArray = [NSMutableArrayarrayWithArray:[tableindexPathsForSelectedRows]];
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)
{
[deleteArrayexchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
}
for (NSIndexPath *indexPathin 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];
[titleArrayremoveObjectAtIndex:i];
}
}
[tablereloadData];
//每次删完后清空数组
[deleteArrayremoveAllObjects];
}
6.#pragma mark ---插入
- (void)pressInsertBtn
{
[dataArray[1]insertObject:@"提莫"atIndex:2];
NSIndexPath *indexPath = [NSIndexPathindexPathForRow:2inSection:1];
//刷新单个section
[tableinsertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationTop];
//刷新整个表格
// [table reloadData];
}
7#pragma mark ---编辑
- (void)pressEditBtn
{
//检测删除数组中是否有元素
if (deleteArray.count)
{
[deleteArrayremoveAllObjects];
}
if (table.editing)
{
[tablesetEditing:NOanimated:YES];
}
else
{
[tablesetEditing:YESanimated: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 = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellID];
/*
UITableViewCellSelectionStyleNone,无选中效果
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray,
UITableViewCellSelectionStyleDefault
*/
//设置cell的选中样式
cell.selectionStyle =UITableViewCellSelectionStyleGray;
/*
UITableViewCellAccessoryNone, 无 默认
UITableViewCellAccessoryDisclosureIndicator, 箭头
UITableViewCellAccessoryDetailDisclosureButton,
UITableViewCellAccessoryCheckmark,
UITableViewCellAccessoryDetailButton
*/
//设置系统的右侧小图标
cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;
//设置自定义的右侧图标(文字)
UIImageView *imageV = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0, 40, 40)];
imageV.image = [UIImageimageNamed:@"1.jpg"];
cell.accessoryView = imageV;
//设置编辑状态下的右侧小图标
cell.editingAccessoryType =UITableViewCellAccessoryCheckmark;
// editingAccessoryView 设置编辑状态下自定义的右侧小图标
//设置自定义的选中的背景图片
UIImageView *backView = [[UIImageViewalloc]init];
backView.image = [UIImageimageNamed:@"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 = [UIColorredColor];
}
else
{
cell.textLabel.textColor = [UIColorblackColor];
}
cell.imageView.image = [UIImageimageNamed: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 = [NSMutableArrayarrayWithObjects:@"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 = [[UILabelalloc] init];
label.text = array[section];
label.backgroundColor = [UIColorredColor];
label.userInteractionEnabled =YES;
label.tag =100 + section;
UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@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)
{
[indexArrayreplaceObjectAtIndex:label.tag -100 withObject:@"0"];
}
else
{
[indexArrayreplaceObjectAtIndex:label.tag -100 withObject:@"1"];
}
//刷新数据表格
[tableVreloadData];
}
- UI第十一天
- iOS-学习笔记-UI-第十一天
- UI第十一天:UITableView ⾼级
- 实战项目:通讯录 UI—第十一天
- 第十一天
- 第十一天
- 第十一天
- 第十一天
- 第十一天
- 第十一天
- 世界杯第十一天
- 第十一天 优先级
- 第十一天:听课笔记
- 第十一天:总结
- 第十一天总结0403
- 5.20 第十一天 坚决
- 星海第十一天
- 实习日记->第十一天
- c++实验2-标准体重
- 大数据时代 摘录一
- php 扩展sqlsrv调用mssql存储过程
- HDU 2094 产生冠军
- 日历生成装置和日历生成方法
- UI第十一天
- 随机数产生转换-根据(1,5)随机数生成器,生成(1,7)之内的随机数
- Linux C高级编程——文件操作之库函数
- 敏捷开发需要编写文档吗?
- java操作阿里云的对象存储OSS
- IT忍者神龟之 Linux必学的60个命令
- 又见01背包(数据较大,重量和价值互换)
- Fragment如何避免onCreateView重复调用
- Java方法签名与JNI,反射