- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath根据你记住的按钮返回相应的类型啊。UITableViewCellEditingStyleDelete或者UITableViewCellEditingStyleInsert。
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell"
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
UIButton btn1 = [[UIBUtton alloc] initwithFrame:CGRectMake(100,5,100,35)]
btn1.tag = indexPath.row
[btn1 addTarget:self action:@selector(btn1clicked:) forControlEvents:UIControlEventTouchUpInside]
[cell addSubview:btn1]
UIButton btn2 = [[UIBUtton alloc] initwithFrame:CGRectMake(300,5,100,35)]
btn2.tag = 1000 + indexPath.row
[btn2 addTarget:self action:@selector(btn2clicked:) forControlEvents:UIControlEventTouchUpInside]
[cell addSubview:btn2]
}
return cell
}
-(void)btn1clicked1:(UIButton *)btn{
NSLog(@"btn1======%d",btn.tag)
}
-(void)btn1clicked2:(UIButton *)btn{
NSLog(@"btn2======%d",btn.tag - 1000)
}
UITableView是app开发中常用到的控件,功能很强大,多用于数据的显示。初始化tableview后,要想显示数据,就必须遵守tableview的数据源代理,而且实现以下方法否则程序会崩溃:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
该方法决定了tableview的每一行显示什么内容,返回值为UITableViewCell,可以通过设置UITableViewCell的各种属性(image,label..)从而达到想要的效果,或者自定义Cell,关于自定义cell在后面会做相关介绍
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
该方法决定了每一组(section)中有几个cell
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
该方法决定了tabview显示几组,这个方法不一定要实现,如果不实现默认显示一组
如果想要实现对tableview 的 *** 作(如:点击每一个cell能获得相应)则必须遵守UITableViewDelegate协议,成为代理
以下是几个比较常用的方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
该方法可以设置每个cell 的高度
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
该方法可以自定义每个组的头部(图片,按钮等等)
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
该方法可以自定义每个组的尾部(图片,按钮等等)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
该方法获取当前点击的cell的indexPath(结构体,包括了section和row)
如何复制粘贴剪切一个cell
遵守UITableViewDelegate协议,并实现以下方法
该方法决定在长按cell后 是否显示 复制粘贴 菜单
-(BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath{
return YES
}
该方法决定 菜单上显示什么按钮
-(BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
// 不显示剪切的按钮
if (action==@selector(copy:)||action==@selector(paste:)) {
return YES
}else{
return NO
}
}
该方法决定 点击菜单上的复制粘贴按钮 后干啥
-(void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
if (action==@selector(copy:)) {
NSLog(@"copy!")
}else if(action==@selector(paste:)){
NSLog(@"paste!")
}
cell 的编辑
设置cell能否被编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES
}
设置删除按钮的文字
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"删除"
}
实现cell 的编辑,通过editingStyle做不同 *** 作
editingStyle:是编辑模式是枚举类型,有以下三种
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete,
UITableViewCellEditingStyleInsert
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
UITableViewCell的使用
创建TableViewCell
UITableViewCell *cell=[UITableViewCell alloc]initWithStyle:<#(UITableViewCellStyle)#>reuseIdentifier:<#(nullable NSString *)#>]
reuseIdentifier:重用标识符,定义重用标识符可以实现cell的复用、
TableViewCell的重用:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newscell"]
//复用重用标识符为newscell的cell
if (cell == nil) {
//没有重用标识符为newsreel的cell 创建一个cell并且设置重用标识符
cell=[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"newsreel"]
}
//注册tableviewcell,这样就不需要对cell进行为空判断
[tableView registerClass:<#(nullable Class)#>forCellReuseIdentifier:<#(nonnull NSString *)#>]
//使用xib创建cell,使用下面的方法进行tableviewcell的注册
[tableView registerNib:<#(nullable UINib *)#>forCellReuseIdentifier:<#(nonnull NSString *)#>]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)