UITableView常用方法

本文详细介绍了UITableView的代理方法及其使用方式,包括如何设置代理、实现数据源和编辑操作等。通过具体实例展示了如何配置单元格、调整单元格高度、启用编辑模式及实现复制粘贴等功能。
1遵守代理协议
    2.设置代理
    self.tableView.delegate = self;
3 实现代理方法

//    3.设置分割线的样式


//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;


//    4.设置分割线的颜色


    self.tableView.separatorColor = [UIColor blueColor];


 


/** * 返回多少组 */ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } /** * 返回多少行 */ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.heros.count; } //返回怎样的cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 1.创建cell UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; // 2.设置数据 HeroModel *hero = self.heros[indexPath.row]; // 2.1设置名称 cell.textLabel.text = hero.name; // 2.2设置图片 cell.imageView.image = [UIImage imageNamed: hero.icon]; // 2.3设置子标题 cell.detailTextLabel.text = hero.intro; // 2.4设置指示器 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 2.4可以用来设置右边显示的视图指示器(必须要设置位置) UISwitch *switch1 = [[UISwitch alloc]init]; UIView *redView = [[UIView alloc]init]; redView.backgroundColor = [UIColor redColor]; cell.accessoryView = redView; return cell; } //可以确定每一cell用不同的高度 //- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ //

//     在这个方法中确定高度.这个方法会在返回怎样的cell方法之前调用

//    if (indexPath.row == 0) {
//        return 100;
//    }else{
//        
//        return 60;
//    }
//    
//}
隐藏状态栏

- (BOOL)prefersStatusBarHidden{
    
    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;
    
}
/**
 *  表示能够让弹出粘贴面板
 *
  */
- (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];
//     2.1给粘贴面板赋值
   
    if (action == @selector(copy:)) {
        [UIPasteboard generalPasteboard].string = hero.icon;
    }
//     2.2如果是粘贴方法,那就修改模型,然后刷新数据(也可以采用局部刷新)
    if (action == @selector(paste:)) {
        
        
        hero.icon = [UIPasteboard generalPasteboard].string;
        
    
        [self.tableView reloadData];
    }
    
    
    
}

//    3.滚动到最后的视图

    NSIndexPath *indexBtnPath = [NSIndexPath indexPathForRow:self.tgs.count-1 inSection:0];

    

    [self.tableView scrollToRowAtIndexPath:indexBtnPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

转载于:https://www.cnblogs.com/gp886/p/4941036.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值